Hello,
I’m started from this example
(#198 Edit Multiple Individually - RailsCasts) to
create a view to update multiple fields.
My goal is permit at the user to update all exam scores in the same
view. I know the students registered at the exam, so I did’t need a
multiple selection. I have showed my DB structure in this post
(Filter on multiple select - Rails - Ruby-Forum).
I see the view in the right way, but the score don’t update. Below, my
code, thanks for the help.
registrations_controller.rb
def update_multi
@registrations = Registration.update(params[:ids].keys,
params[:registrations].values).reject { |p| p.errors.empty? }
if @registrations.empty?
format.html { redirect_to @registration.exam, notice: ‘Exam
Updated.’ }
else
redirect_to @registration.exam
end
end
routes.rb
resources :registrations do
collection do
put ‘update_multi’
end
end
resources :exams do
member do
get ‘score’
end
end
exam_controller.rb
def score
@exam = Exam.find(params[:id])
@registrations = Registration.where(“exam_id = ?”, params[:id])
end
exams/score.html.erb
…code to show data exam…
<%= form_tag ‘registrations/update_multi’, :method => :put do %>
<% for reg in @registrations %> <%= fields_for "registrations[]", reg do |r| %> <% end %> <% end %>Name | Score |
---|---|
<%= reg.student.name %> | <%= render "/registrations/form_one", :r => r %> |
registrations/_form_one.html.erb
<%= r.text_field :vote %>