Can't create a form for comments in a post RAILS -
I am trying to create a comment box / form under my article in Rail, but this unknown feature: Post_id < / P>
My form code
& lt;% = form_for ([@ post, @ post.comments.build]) do | F | & Gt%; & Lt; P & gt; & Lt;% = f.label: Commentator% & gt; & Lt; Br> & Lt;% = f.text_field: Commenter% & gt; & Lt; / P & gt; & Lt; P & gt; & Lt;% = f.label: body% & gt; & Lt; Br> & Lt;% = f.text_area: Body% & gt; & Lt; / P & gt; & Lt; P & gt; & Lt;% = f.submit% & gt; & Lt; / P & gt; & Lt;% end% & gt;
For making my comment,
def make @ post = post.fund (param [: post_id]) @ comment = @ post quotations. Build (Comment_params) redirect_to post_path (@ post) response_to do | Format | If @ comment.save format.html {redirect_to @ comment, notice: 'The comment was made successfully.' } Format.json {Render Action: 'Show', Status :: created, Location: @Content} and format. Html {Action: 'new'} format.json {json: @ comment: error, status :: unprocessable_entity} and end
& lt ;% = Form_for ([@post, @ post.comments.build]) raises an error. F | & Gt%;
Unknown attribute is saying: post_id
thanks all!
Renewed error: Undefined method `Post '
& lt; TR & gt; & Lt; Td> & Lt;% = comment.commenter% & gt; & Lt; / Td> & Lt; Td> & Lt;% = comment.body% & gt; & Lt; / Td> & Lt; Td> & Lt;% = comment.posts% & gt; & Lt; / Td> & Lt; Td> & Lt;% = link_to 'Show', comment% & gt; & Lt; / Td> & Lt; Td> & Lt;% = link_to 'edit', edit_comment_path (comment)%> & Lt; / Td> & Lt; Td> & Lt;% = link_to 'deleted', comment, method :: delete, data: {confirm: 'are you sure?' }% & Gt; & Lt; / Td>
It looks like you do not have post_id
Code> Comment is causing the error in the table.
By looking at my code, I guess you want 1-M relationships between posts
and comments
models.
Ensure that the relationship is set correctly in the model below:
class post < ActiveRecord :: Base is has_many: Comments ## ... End class comment & lt; After this, you will have to create the post_id
column in the comments
table, then ActiveRecord :: Base is_to: post ## ... end
This should solve your unknown attribute: post_id
error as the reference of the post
table.
You can generate the migration and create comments
table in the post_id
column:
after this run < Code> rake db: migrate
UPDATE
me Above the problem of how to upgrade
, you must have Rated root in the routes. Rb
resources: resources are the resources: comments ending
update 2
< P> Change & lt; Td> & Lt;% = comment.posts% & gt; & Lt; / Td>
To
& lt; Td> & Lt;% = comment.post% & gt; & Lt; / Td>
Note singular posted
and plural posted
not
Comments
Post a Comment