This code works, but I think the solution is ugly, please advise.
User updates a standard form_for, the form is re-rendered with
checkboxes next to each updated line. The user is expected to select
up to 3 checkboxes which will be saved in the user model under columns
(top_1of3, top_2of3, top_3of3).
View:
<% form_tag :action => ‘top_three’ do %>
<% for line in @updated_info do %>
<%= check_box_tag( “choice_” + @updated_info.index(line).to_s ,
line ) %><%= line %>
<% end %>
<%= submit_tag %>
<% end %>
Controller:
choices = {}
for i in 0..50
choice = "choice_" + i.to_s
choice = choice.intern
if !params[choice].nil? and choices["top_1of3"].nil?
choices["top_1of3"] = params[choice]
elsif !params[choice].nil? and choices["top_2of3"].nil?
choices["top_2of3"] = params[choice]
elsif !params[choice].nil? and choices["top_3of3"].nil?
choices["top_3of3"] = params[choice]
end
end
if @user.update_attributes(choices)
…