我希望有人可以帮我解决一个我想做的简单形式的关联。 我想要做的是为音乐视频创建一个simple_form,该视频具有多个通过的关联形式。我的架构如下所示:
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_video_taggings有许多标签,反之亦然。但我对如何完成与simple_form的关联感到困惑。我有一个new.html.erb,表单如下:
<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 %>
现在我知道,我应该为f.input:name设置关联,但我不想要复选框,选择或收音机。我怎样才能做到这一点?我想在表单的顶部为@music_videos和@tags创建一个表单,但我认为这不会起作用。我一直收到的错误是有一个未定义的方法名称。有任何想法吗?我已经坚持了一段时间。非常感谢一些帮助。感谢。
答案 0 :(得分:3)
替换
<%= f.input :name, label: "Tag" %>
使用
<%= f.simple_fields_for :tags do |p| %>
<%= p.input :name, label: "Tag" %>
<% end %>
有关详细信息,请参阅简单表单中的 Nested Models 。