controller/view problem from RoR n00b

can anybody tell me why my controller is not creating a new comment, but my view lists comments that i manually add to the database?

here is my view code:

  <% for comment in @post.post_comments %>     <%= comment.body %>     <%= comment.email_address %>   <% end %>

  <%= form_tag :action => "comment", :id => @post %>   <%= text_area "post_comment", "body" %><br/>   <%= submit_tag "Comment!" %>   </form>

and here is my controller code:

  def comment     Post.find(params[:id]).post_comments.create(params[:post_comment])     flash[:notice] = "Added your comment."     redirect_to :action => "show", :id => params[:id]   end

i have models called post and post_comment and tables called posts and post_comments.

i don't get any error messages and it redirects back to the appropriate post on the show page, but the flash doesn't work and the comment isn't added.

Hey Luke,

your problem lies with Instance versus Class methods.

>Post.find(params[:id]).post_comments.create(params[:post_comment])

Post.find returns an array of post instances. The instance method post_comments returns an array of comments instances.

The create method doesn't belong either to a particular comment instance, nor to Array.

You should be calling Post.create and adjusting your params as such.

Plus if you check your development.log, you'll likely see something like "create is not a method of Array". This log should be a key source of debugging.

J

The above post is not correct.

Post.find only returns a single object if only one is found (i.e. when finding by params[:id] as above).

post.post_comments is an association proxy that will return an array of related PostComment objects *but*, you can also call methods such as create on it as well. See the API docs for more information.

The above post is not correct.

Post.find only returns a single object if only one is found (i.e. when finding by params[:id] as above).

post.post_comments is an association proxy that will return an array of related PostComment objects *but*, you can also call methods such as create on it as well. See the API docs for more information.

I stand corrected. Thank you.

This is one area of the documentation (IMO) is weak . But consulting through irb, and .methods .instance_methods ljredpath is correct.

J

Thanks for the response. I follow you on the first part where you say "Post.find only returns a single object if only one is found (i.e. when finding by params[:id] as above).", but I lost you on the second part. I will definitely check the API documentation, but I'm not sure where to look. Can you explain the second part in a little more detail or refer me to the proper area of the API docs?

Thanks,

Luke

ljredpath@gmail.com wrote:

If you look at:

You'll see, under the section for has_many, a list of the methods available.

If you look at:

ActiveRecord::Associations::ClassMethods

You'll see, under the section for has_many, a list of the methods available.

as sure as Bob's your uncle, I stand corrected again. I hope I'm not making a habit of this!

J

Got it now!! I had a validates_presence_of in my model for a field that I had deleted from the table. Anyone have any idea why rails doesn't tell you when you make a dumb mistake like that??