Complete beginner to Rails here:
In Rails:
I have a Model Post, which hasmany Tags. When creating a new Post, I want the user to be able to create up to 5 tags that are tied to the Post.
I set up the form to create a new Post like this:
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
... Some more of these
<div class="field"> <!-- I want this to refer to the name attribute of a Tag model-->
<%= f.label :tag_name %><br />
<%= f.text_field :tag_name %>
</div>
<% end %>
Obviously, this doesn’t work since the Post class does not have a tag_name attribute. What’s the proper way to do this?
Assume that Tag is an aggregation table that has the following fields:
id: primary key
post_id: foreign key to Post's primary key
name: name of the tag
Check out this railscast.
Basically, you need accepts_nested_attributes_for and then fields_for.