Using the keyword “references” in my rails scaffold command usually gives me errors, and I think this is the reason why.
Following up on one of my favorite rails tutorial: AkitaOnRails
I run the command
./script/generate scaffold Comment post:references body:text
and the scaffold generator will create something like this in my views/new html file.
<%= f.label :post %><br />
<%= f.text_field :post %>
The problem here is, if you actually try to run that code without any modifications, you will get something like this in return
This is probably because rails is expecting you to pass in the associated object, in this case the associated Post object, but we are just sending it the ID of the object as a string.
To fix this, all you do is change all the references to (in the view files)
I think this happened because when they added the “references” keyword, they forgot to update the scaffold generator to reflect the fact that “post” get converted to “post_id” during migration.
Technically speaking, if you were more advanced in Rails, you could build the proper associations in your model/controller and make this work “properly”, but as a noob starting out on rails and just relying on scaffold for immediately satisfaction, this could be a total bummer…
This VinhCasts was brought to you by… hehehehe