Help with forms and joined tables?

Hello all,

I know this is something that should be almost ridiculously easy (I am
new with RoR, so please be gentle). I am trying to create a comment on
a simple forum posting, but am having problems setting up my form
properly. I am using Rails 2.0.2, but everything I find online seems to
use the now-deprecated 'start_form_tag' and I'm not having much luck
converting it to 'form_for'.

Here is my code in the show.html.erb file:

21: <% form_for :comment, :url => { :action => "add_comment", :id =>
@post } do |f| %>
22: Add a comment:<br>
23: <%= f.text_area :comment %>
24: <br>
25: <%= submit_tag%>
26: <% end %>

And here is my code in the posts_controller:

  def add_comment

    flash[:notice] = 'Comment added to the dream!'
    redirect_to :action=>'show', :id=>params[:id]

Can somebody point out my mistake? TIA

Sorry about that...reading back through it I see that I never actually
said what the problem was. I have modified it per your instructions,
but the end result is still the same as the error has something to do
with the hash that is getting passed (I think).

Here is the updated code for the show page:

   <% form_for :comment, :url => { :action => "add_comment", :id =>
@post } do |f| %>
     Add a comment:<br />
     <%= f.text_area :comment %>
     <br />
     <%= f.submit "Save" %>
   <% end %>

And here is what I have in the Post controller:

  def add_comment

    flash[:notice] = 'Comment added'
    redirect_to :action=>'show', :id=>params[:id]

I attached the error log for this action, but here is the line that
catches my attention:

NoMethodError (undefined method `comment=' for #<Comment:0xb7ad0c34>):

I don't quite understand why it thinks 'comment=' is a method?

Thanks for all your help, I really appreciate this.


I had a similar problem the other day. Change your form tag to this:

<%= form_tag :controller => 'YourController', :action => 'add_comment'

where YourController is... well, the name of your controller. End it

<%= end_form_tag -%>

using this method, you don't need to use the <%= f.text_area %> thing,
but instead just use regular HTML textboxes. If you specify your HTML
textbox name to be 'comment', you can access it in your controller by
using params[:comment].

However, I'm pretty sure you still have to use the ruby submit button:

<%= submit_tag 'save' -%>

Give it a whirl, hopefully it works for you. I'm pretty new to RoR too,
but this worked for me.

- Jeff

Hey guys, that got me on the right track! I used the form_tag, but had
to remove the '=' and also add a 'do' for it to work. I also just used
a HTML textarea element named 'comment'. That got the view corrected,
but I then started receiving an error saying:

undefined method 'stringify keys!' for "text added for comment

To remedy that, I updated the controller so that the field to which I
was inserting (body) was explicitly named like this:

    Post.find(params[:id]).comments.create(:body => params[:comment])

Thanks Jeff and Ilan for the assist!


Derek Knorr wrote:

    Post.find(params[:id]).comments.create(:body => params[:comment])

This suggests your Comment model's attribute is "body". It seems
unlikely that you have a special page just to create a comment, so I'm
going to assume that this form is on the Post show view. What you
probably need then is something like this:

Post controller:


That was an excellent explanation. This is the way I pictured it
working, just didn't know how to get there from where I was at. It's
starting to make sense!