I’ve a couple of model I’ve made using the scaffold generator.
There are:
rails g scaffold event title:string content:text
rails g scaffold link title:string
I would like that each event has many links, and link belongs to events.
So in the event models I’ve coded:
has_many :links
And inside the link model:
belongs_to :event
Moving on I’ve run:
rake db:migrate
In order build the database. And I’ve made a couple of events using scaffold feature.
Now I’d like to add links directly inside the event’s show>views.
Inside the show.html.erb (inside the view>events dir) I’ve written:
<%= form_for([@event, @event.links.build]) do |f| %>
<p><%= f.text_field %></p>
<p><%= f.submit "Add the link" %></p>
<% end %>
But when I try to visit the show page the browser says:
unknown attribute: event_id
And it says the error is on this line:
<%= form_for([@event, @event.links.build]) do |f| %>
What I did wrong?
Thanks in advance
You must create the foreign key in your migrations, the
event_id, by changing the existing ones or creating a new one.Since you have already migrate them it’s recommended you create a new one, so it should be something like this
Edit: Don’t forget that to create the migration file you should use the command
Or any other name then AddEventForeignKeyToLinks
routes