Hello,
I’m started from this example
(http://railscasts.com/episodes/198-edit-multiple-individually) 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
(http://www.ruby-forum.com/topic/4409047#new).
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 %>
Name | Score |
---|---|
<%= reg.student.name %> | <%= fields_for "registrations[]", reg do |r| %><%= render "/registrations/form_one", :r => r %> | <% end %>
registrations/_form_one.html.erb
<%= r.text_field :vote %>