Question about making comment in blog system

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’ %>

<%= 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 :slight_smile:

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

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