Question

I was hoping someone could please help me with a simple_form association that I'm trying to do. What I want to do is to create a simple_form for a music video that that has an association in the form of a has many through. My schema looks like this:

create_table "music_videos", force: true do |t|
t.string  "song_title",  null: false
t.string  "venue",       null: false
t.string  "location",    null: false
t.string  "description", null: false
t.string  "url_id",      null: false
t.integer "user_id",     null: false
end

create_table "music_videos_tags", id: false, force: true do |t|
  t.integer "music_video_id", null: false
  t.integer "tag_id",         null: false
end

add_index "music_videos_tags", ["music_video_id"], name:     
index_music_videos_tags_on_music_video_id", using: :btree
add_index "music_videos_tags", ["tag_id"], name: "index_music_videos_tags_on_tag_id",   
using: :btree

create_table "tags", force: true do |t|
  t.string "name", null: false
end

Music Videos has many tags through music_video_taggings vice versa. But I'm confused on how to do accomplish the association with simple_form. I have a new.html.erb with a form like so:

<h1>Submit Your Video</h1>

<%= simple_form_for @music_video url: music_videos_path, method: :post do |f| %>
 <%= f.input :song_title %>
 <%= f.input :venue %>
 <%= f.input :location, label: "City", collection: MusicVideo::CITY %>
 <%= f.input :description %>
 <%= f.input :url_id, label: "Paste url id ex. 82237472" %>
 <%= f.input :name, label: "Tag" %>
 <%= f.submit %>

Now I know, I'm supposed to put an association for f.input :name, but I don't want checkboxes, selects, or radios. How can I do this? I thought about creating a form for both @music_videos, and @tags right at the top of the form, but I don't think that will work though.The errors that I keep receiving are that there is an undefined method name. Any ideas? I've been stuck on this for awhile. Some help would greatly be appreciated. Thanks.

Was it helpful?

Solution

Replace

 <%= f.input :name, label: "Tag" %>

With

  <%= f.simple_fields_for :tags do |p| %>
    <%= p.input :name, label: "Tag"  %>
  <% end %>

Refer to Nested Models in Simple Form for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top