【问题标题】:Rails4 - Displaying items in the orders formRails4 - 在订单表单中显示项目
【发布时间】:2014-05-26 17:54:00
【问题描述】:

我希望用户从订单表单中的项目表中搜索现有项目,它适用于客户但不适用于项目,它会给出错误:Association :item not found

型号

class Order < ActiveRecord::Base
    belongs_to :user
    belongs_to :client

    has_many :order_items
    has_many :items, :through => :order_items
end

class Item < ActiveRecord::Base
    has_many :order_items
    has_many :orders, :through => :order_items
end

class OrderItem < ActiveRecord::Base
    belongs_to :item
    belongs_to :order
end

迁移

class CreateOrderItems < ActiveRecord::Migration
  def change
    create_table :order_items do |t|
        t.integer :item_id
        t.integer :order_id
      t.timestamps
    end
    add_index   :order_items, [:item_id, :order_id]
  end
end

查看

<%= simple_form_for(@order) do |f| %>
  <%= f.error_notification %>
    <%= f.association :client, collection: Client.all, label_method: :name, value_method: :id, prompt: "Choose a Client", input_html: { id: 'client-select2' } %>
    <%= f.association :item, collection: Item.all, label_method: :name, value_method: :id, prompt: "Choose an item", input_html: { id: 'client-select2' }  %>
<%= f.input :memo, label: 'Comments' %>
    <%= f.submit %>
<% end %>

控制器

 def new
    @order = Order.new
  end

  def create
    @order = Order.new(order_params)
    @order.user_id = current_user.id
    @order.status = TRUE
  end
  def order_params
    params.require(:order).permit(:code, :client_id, :user_id, :memo, :status,    items_attributes: [:id, :name, :price, :quantity, :status, :_destroy])
  end

回答

在表格中使用: 使用 rails-select2 gem

<%= f.association :items, collection: Item.all, label_method: :name, value_method: :id, prompt: "Choose an item", input_html: { id: 'item-select2' } %>

或者没有select2

<%= f.select :item_ids, Item.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>

感谢 JKen13579

【问题讨论】:

  • 为什么要添加has_many :order_itemshas_many :items, :through =&gt; :order_items?你应该只在 AFAIK 上添加第二个。
  • 另外belongs_to :userbelongs_to :client 看起来很奇怪,用户实际上不是客户吗?
  • 我指的是:link 他们有 has_many,所以我想我也需要它
  • 好吧,用户是为客户创建订单的员工。客户不接触系统。
  • 啊哈,我明白了,那么关于关联逻辑一切都很好:)

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


【解决方案1】:

您收到item 的错误但未收到client 的错误的原因是有一个client 与一个订单相关联,但有多个item 与一个订单相关联。它说:item not found,因为你应该使用:items(注意复数)。

要允许您的orderitems 进行多选,请将您的f.association item 行替换为:

<%= f.select :item_ids, Item.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>

然后在你的控制器中,确保允许item_ids。另外,你不需要item_attributes,因为你没有使用accepted_nested_attributes_for :items

def order_params
  params.require(:order).permit(:code, :client_id, :user_id, :memo, :status, item_ids: [])
end

有关has_many :through 多选的更多信息,请参阅this SO answer

【讨论】:

  • :items 工作,没有显示错误,但是在我选择项目的同时提交表单后,表格 :order_items 仍然显示为空
  • 啊,是的,您正在使用强参数。它在the SO answer I posted 中,但我将它添加到我的答案中。希望能为您解决问题。
  • 效果很好。 &lt;%= f.association :items, collection: Item.all, label_method: :name, value_method: :id, prompt: "Choose an item", input_html: { id: 'item-select2' } %&gt; 是我用来代替搜索的多选(rails-select2 gem)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多