將三個微調器字段添加到您的表單中,並使用主題標識符作爲數據和主題名稱作爲標籤填充它們。幸運的是,有幫助你完成大部分重任的人。 See here有關collection_select
的詳細信息。下面是該鏈接取一個例子:
<%= collection_select(:person, :city_id, City.all, :id, :name) %>
在你的控制器,你可以創建基於選擇的IDS必要的關聯。它應該是這個樣子:
_form.html.erb
<% form_for @article do |f| %>
...
<%= collection_select(:article, :topic_id_1, Topic.all, :id, :name) %>
<%= collection_select(:article, :topic_id_2, Topic.all, :id, :name) %>
<%= collection_select(:article, :topic_id_3, Topic.all, :id, :name) %>
...
<% end %>
acticle_controller.rb
def create
...
@article.topics << Topic.find params[:topic_id_1]
@article.topics << Topic.find params[:topic_id_2]
@article.topics << Topic.find params[:topic_id_3]
...
end