Forum: Ruby on Rails Submitting data from view to controller without using form o

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
AdamSebWolf (Guest)
on 2007-02-06 18:04
(Received via mailing list)
Hi Guys,
My first post (and second RoR app ever!) so expect an easy problem,

I've been making a blog (suprise suprise, my first app was a webstore
too!), and I've got a charming front page, listing my posts with some
nice css and a really clean interface. Beneath each post I have a
comments form, with a name field, a textarea for the body, and a
submit post button. Easy!
The form's action is set to call the 'add_comment' action in my
controller, and the input fields are set to correspond to comment.name
and comment.body in the Comment object. This all works perfectly! I
pull out the comment object from params with params[:comment] and toss
it into the database with comment.save, only to see exceptions that I
haven't told the comment which post it belongs to! (post_id is not
null, see below)
In the comments table in my database, there are: id, post_id, name,
body. post_id is linked to the posts table by id, and I've done all
the work to set this up correctly.

My question is:
Does the Comment object have a post_id property from the table?
If so, how do I set it from within my blog.rhtml page? I currently
have a "for post in @posts" loop, and so post corresponds to the
relavent post_id. Bear in mind that I'm using multiple duplicate
forms.
If not, how do I set the post_id property?

Thanks a lot, I'm sure this is a really obvious one I've totally
missed,
Sebastian Wolf
Kim (Guest)
on 2007-02-06 19:46
(Received via mailing list)
It would help to see the relevant code.
Bill S. (Guest)
on 2007-02-06 20:20
(Received via mailing list)
Yes, there will be a 'post_id' property on your comment ... one way
would be to set a hidden field on your form holding the post id. There
are about 10 other ways of doing this ...
This topic is locked and can not be replied to.