0

次の関連付けを持つ製品モデルとカテゴリモデルの 2 つのモデルがあります。

class Product < ActiveRecord::Base
  belongs_to :category

  validates :title, presence: true
end

class Category < ActiveRecord::Base
  attr_accessible :name
  has_many :products
end

simple_form を使用して新しい製品を作成しようとすると、category_id フィールドに、カテゴリの ID ではなく、カテゴリの名前が必要です。

<%= simple_form_for @product do |f| %>
    <%= f.input :title %>
    <%= f.input :description %>
    <%= f.input :price %>
    <%= f.input :category_id %>
    <%= f.button :submit %>
<% end %>

どうすればいいですか?

4

1 に答える 1

2

次のようなものがあると思います:

<%= simple_form_for @product do |f| %>
    <%= f.input :title %>
    <%= f.input :description %>
    <%= f.input :price %>
    <%= f.association :category, collection: 
                   Category.all, prompt: "Choose a category"%>

    <%= f.button :submit %>
<% end %>
于 2013-06-16T14:12:17.173 に答える