I''m a newbie in Rails and I''m follwing some tutorials right now. Right now, I''m following "Creating a weblog in 15 minutes" that posted on rubyonrails.org site, but there is onething that I don''t understand. According to the demo movie, when posting a comment under a blog entry, write following codes: def add_comment Post.find(params[:id]).comments.create(params[:comment]) flash[:notice] = "Added your comment." end and then in the view file, <%= start_form_tag :action => ''comment'', :id => @post %> <%= text_area ''comment'', ''body'' %><br /> <%= submit_tag ''Comment!'' %> <% end_form_tag %> What I don''t understand is, if we have more column in the comment table, for example ''comment_poster_id'' or ''commented_time'', then where do I assign the value? Logically, I think it should be within the controller file, not in the view file, but I don''t know how. Could you someone tell me how can I do that? Thanks :) -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
The create() method takes in a hash containing keys names the same as fields in the respresentative table. In this case, you''ll probably be passing in: params = { :post => { :id => [postid] }, { :comment => { :body => ''some body text'' } } Thus, you can just modify the poster form to include a comment_poster_id field which will automatically be added into the hash for you. However if you need to add such values in the Controller, you can do this: def add_comment comm = Comment.new(params[:comment]) comm.poster_id = poster_id comm.commented_time = Date.today # or whatever Post.find(params[:id]).comments << comm # I think this saves for you but I can''t remember. end There are many different ways of manipulating ActiveRecord objects. If in doubt, just seperate out into seperate objects and do what you need. Hope that helps. Jason On 9/13/06, Joonhwan Lee <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I''m a newbie in Rails and I''m follwing some tutorials right now. > Right now, I''m following "Creating a weblog in 15 minutes" that posted > on rubyonrails.org site, but there is onething that I don''t understand. > > According to the demo movie, when posting a comment under a blog entry, > write following codes: > > def add_comment > Post.find(params[:id]).comments.create(params[:comment]) > flash[:notice] = "Added your comment." > end > > and then in the view file, > > <%= start_form_tag :action => ''comment'', :id => @post %> > <%= text_area ''comment'', ''body'' %><br /> > <%= submit_tag ''Comment!'' %> > <% end_form_tag %> > > What I don''t understand is, if we have more column in the comment table, > for example ''comment_poster_id'' or ''commented_time'', then where do I > assign the value? > Logically, I think it should be within the controller file, not in the > view file, but I don''t know how. > Could you someone tell me how can I do that? Thanks :) > > -- > Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
For comm.commented_time, you can use ActiveRecord''s "magic" created_at column, which will default to the time the Record is created without you having to do anything. Just call the column "created_at" and make sure it can handle a timestamp (string works at worst.) Louis On 9/13/06, Jason Roelofs <jameskilton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > The create() method takes in a hash containing keys names the same as > fields in the respresentative table. In this case, you''ll probably be > passing in: > > params = { :post => { :id => [postid] }, { :comment => { :body => ''some > body text'' } } > > Thus, you can just modify the poster form to include a comment_poster_id > field which will automatically be added into the hash for you. > > However if you need to add such values in the Controller, you can do this: > > > def add_comment > comm = Comment.new(params[:comment]) > comm.poster_id = poster_id > comm.commented_time = Date.today # or whatever > > Post.find(params[:id]).comments << comm # I think this saves for you > but I can''t remember. > end > > There are many different ways of manipulating ActiveRecord objects. If in > doubt, just seperate out into seperate objects and do what you need. > > Hope that helps. > > Jason > > > On 9/13/06, Joonhwan Lee <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > I''m a newbie in Rails and I''m follwing some tutorials right now. > > Right now, I''m following "Creating a weblog in 15 minutes" that posted > > on rubyonrails.org site, but there is onething that I don''t understand. > > > > According to the demo movie, when posting a comment under a blog entry, > > write following codes: > > > > def add_comment > > Post.find(params[:id]).comments.create(params[:comment]) > > flash[:notice] = "Added your comment." > > end > > > > and then in the view file, > > > > <%= start_form_tag :action => ''comment'', :id => @post %> > > <%= text_area ''comment'', ''body'' %><br /> > > <%= submit_tag ''Comment!'' %> > > <% end_form_tag %> > > > > What I don''t understand is, if we have more column in the comment table, > > for example ''comment_poster_id'' or ''commented_time'', then where do I > > assign the value? > > Logically, I think it should be within the controller file, not in the > > view file, but I don''t know how. > > Could you someone tell me how can I do that? Thanks :) > > > > -- > > Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---