【问题标题】:Simple_form association helperSimple_form 关联助手
【发布时间】:2014-06-08 12:11:12
【问题描述】:

我有一个 Product 模型和一个 Size 模型。我想以我的简单形式显示每个产品可用尺寸的下拉列表。我想我需要使用 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

大小.rb

class Size < ActiveRecord::Base

    belongs_to :product

end

products/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 %>

【问题讨论】:

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


    【解决方案1】:

    试试这个;

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

    【讨论】:

    • 太好了,谢谢,唯一的问题是它显示的是哈希值而不是人类可读的实际大小?
    • @James,我不知道你的尺码型号。分享它。可能是 product.sizes.size
    • 谢谢,我刚刚将这个添加到我的尺寸模型def to_s "#{size}" end
    • @James Great,您可以接受它是一个可行的答案,这样就可以避免下一个人的喧嚣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多