So i’m making a web app using ROR and I can’t figure out what the right syntax for this form is. I’m currently making an association type of code for comments and posts.
<%= form_for @comment do |f| %>
<p>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.label :comment %><br />
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit "Add Comment" %>
</p>
<% end %>
Your form is fine, except the first line (and you don’t need a hidden field for the user_id, thats done through your relationship):
Should be:
Now you render a form for creating or updating a comment for a particular post.
However, you should change your model and controller.
This will give you access to @post.comments, showing all comments belonging to a particular post.
In your controller you can access comments for a specific post:
This way you can access the index of comments for a particular post.
Update
One more thing, your routes should also look like this:
This will give you access to post_comments_path (and many more routes)