Hi I am new to RoR and practice some ruby/rails language feathers on my toy application. Here is a question about form_for tag in rails. == the story People profile can be modified by end user, so there is method called "update_profile" in the controller. == the view <% form_for :person,@person, :url => ....do |f|%> <%f.hidden_field :id %> <%f.text_field :ppl_name%> ... <% end%> == the controller def update_profile person = Person.new(params[:person]) puts person.id person.new_record = false; person.save end When I print person.id out, the value is nil. However, i can set the id using below code person.id = params[:ppl_id] Can someone explain why so weird? Thanks --jack --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 9, 2:35 am, "jack.tang" <him...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > I am new to RoR and practice some ruby/rails language feathers on my > toy application. Here is a question about form_for tag in rails. > > == the story > People profile can be modified by end user, so there is method called > "update_profile" in the controller. > > == the view > > <% form_for :person,@person, :url => ....do |f|%> > <%f.hidden_field :id %> > <%f.text_field :ppl_name%> > ... > <% end%> > > == the controller > def update_profile > person = Person.new(params[:person]) > puts person.id > > person.new_record = false; > person.save > end > > When I print person.id out, the value is nil. However, i can set the > id using below code > person.id = params[:ppl_id] > Can someone explain why so weird? Thanks > > --jackI think if you are using form_for you need to use update_attributes() to update the record --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 9, 1:35 am, "jack.tang" <him...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > == the controller > def update_profile > person = Person.new(params[:person]) > puts person.id > > person.new_record = false; > person.save > endYou don''t want to create a new Person, do you? You want to just find the existing person and update it: person = Person.find(params[:id]) person.update_attributes(params[:person]) And also, new_record is generally something you don''t want to touch - rails will control it automatically. Jeff softiesonrails.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---