Simple_form关联助手

时间:2014-06-08 12:11:12

标签: ruby-on-rails ruby-on-rails-4 simple-form

我有一个产品型号和一个尺寸型号。我希望以每种产品可用尺寸的简单形式显示下拉列表。我想我需要使用simple_form提供的关联助手,但我不能让它工作。有人可以帮忙吗?感谢

我得到的错误是:

Association cannot be used in forms not associated with an object

Product.rb

class Product < ActiveRecord::Base

    has_many :order_products
    has_many :orders, through: :order_products
    has_many :sizes

end

Size.rb

class Size < ActiveRecord::Base

    belongs_to :product

end

产品/ show.html.erb

<%= simple_form_for :basket, url: product_basket_path(@product), remote: true do |f| %>

<%= f.input :quantity, as: :select, collection: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], selected: 1, required: false %>

<%= f.association :size %>


<%= f.button :submit, "Add to basket" %>

<% end %>

1 个答案:

答案 0 :(得分:0)

试试这个;

<%= f.input :size, as: :select, collection: @product.sizes."row_name", required: false %>