0
私はこれがあります。同じの内側にさらに2つ以上のレコードを作成する方法についての私のフォーム内の二collection_select
<%= f.collection_select :category_ids, Category.all, :id, :name, {} %>
をし、それが記録には問題を作成していない、しかし私は迷ってしまいました私が試したことは、ただ一つのレコードを作成するか、まったく作成しないことです。
<%= f.fields_for :category_ids do |category| %>
<%= category.collection_select(nil, Category.all, :id, :name,
{include_blank: "---", selected: 0},
{id: :event_category_id_1}) %>
<%= category.collection_select(nil, Category.all, :id, :name,
{include_blank: "---", selected: 0},
{id: :event_category_id_2}) %>
<% end %>
が、今回はそれがまったくレコードを作成していない:私は(私は本当にそれがnilを使用する理由には得ることはありませんaltough)私が分析し、確信していたこのソリューションは、働くつもりでした。
ここでは私のモデルは以下のとおりです。
class Event < ApplicationRecord
has_many :categorizations
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categorizations
end
class Category < ApplicationRecord
has_many :categorizations
has_many :events, through: :categorizations
end
class Categorization < ApplicationRecord
belongs_to :event
belongs_to :category
end
もちろん、UIを改善し、選択したカテゴリを選択タグの下のリストにコピーすることができます。 –
私はあなたが複数の:オプションを使用することができることを見てきましたが、ユーザーが複数を選択することは少し難しいと思います.2つの異なるレコードを作成する2つのcollection_select(または単純な選択)同じフォーム? – Bluespheal
ああ、あなたは 'has_many through'を持っています、あなたのfields_forは':categorizations'のためである必要があります。思考帳でそれらの方法を読んでください。https://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through –