我有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 %>
我该怎么做呢?
我相信你会有这样的东西:
<%= 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 %>