I’m a newbie to rails and am having some difficulty…
I have a page displaying a list of records in a table and would like for the user to be able to make changes, submit the form, to run validation and persist the data.
This is what I have so far:
View:
- @people.each do |p|
%tr
%td
%input{:type => "hidden", :name => "person_id[]", :value => p.id}
%input{:name => "firstname[]", :value => p.firstname}
%td
%input{:name => "lastname[]", :value => p.lastname}
Example parameters being posted to the controller:
"person_id"=>["12", "13", "14"],
"firstname"=>["john", "joe", "mary"],
"lastname"=>["smith", "bloggs", "jane"],
At this point I am scared, because I am no longer bound to an active record. Instead I feel myself wanting to write some messy code to loop over the person_id array to see what has changed and write any changes back.
This feels bad because I have to explicitly compare each field, also if something fails due to a validation error half way through how should I rollback any changes and display the messages to the user?
I’m hoping that due to my rails ignorance this whole approach is wrong and I am missing a trick. Does anyone have any suggestions for how to approach this problem?
I suppose you have an association setup between
people modelandperson model, by looking at the hiddenperson_idfield in your view code.If your associations are rightly setup, use
and follow the Rails guides for association basics.
At this point of time, I can only help you this much, as not much information is provided in your question.