Cocoon gem、collection_select 不适用于 drink_id

Cocoon gem, collection_select don't work with drink_id

我的结构使用 Cocoon gem,在我的 app/views/order/_form.html.erb 下面的代码工作正常:

<%= f.collection_select(:drink_id, @drinks, :id, :name, :prompt => "Select a drink") %>

但是当我在 _drink_fields.html.erb 中使用时不起作用,return 这个:

undefined method `drink_id' for # Drink:0x007fd30f9799f8

app/views/orders/_drink_fields.html.erb

  <div class='nested-fields'>
<div class="field">
  <%= f.collection_select(:drink_id, @drinks, :id, :name, prompt: 'Select a drink') %>
</div>
<%= link_to_remove_association "remove", f %>
</div>

app/views/orders/_form.html.erb

  <hr>
  <h3>Drinks</h3>
  <div id='drinks'>
    <%= f.fields_for :drinks do |drink| %>
      <%= render 'drink_fields', :f => drink %>
    <% end %>
    <div class='links'>
      <%= link_to_add_association 'add', f, :drinks %>
    </div>
  </div>
  <hr>

app/views/models/order.rb

class Order < ApplicationRecord
  belongs_to :drink, optional: true
  has_many :drinks
  accepts_nested_attributes_for :drinks, allow_destroy: true
end

app/controllers/orders_controller.rb

def set_order
  @order = Order.find(params[:id])
end

def set_drink
  @drinks = Drink.all
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
  params.require(:order).permit(:number, :drink_id, drinks_attributes: [:id, :name, :_destroy])
end

为什么我收到了? 如何做好工作?

谢谢大家!!

它不起作用的原因是,@drinks 对象上没有 drink_id 方法(应该与数据库中的 drink_id 列相对应)。您可能想使用 :id 而不是 :drink_id

在检查您的关联时,我认为您在那里犯了一个错误。我看到以下

  • 一个订单属于饮料
  • 一个订单有-很多饮料
  • 那么接下来就是:来一杯属于点单的吧。那么一杯饮料只能点一份吗?

我猜您缺少点菜和饮料之间的连接 table。所以添加一个简单的

class OrderedDrink < ActiveRecord::Base
  belongs_to :order
  belongs_to :drink 
end

您还需要 table,因此您必须添加迁移才能实现(很明显)。 (您可以在那里购买:例如,他们订购了 5 杯可乐)。

然后在Order你得到

 class Order
   has_many :ordered_drinks, inverse_of: :order 
   has_many :drinks, through: :ordered_drinks

   accepts_nested_attributes_for :ordered_drinks 
 end 

Drink 你写

 class Drink 
   has_many :ordered_drinks
   has_many :orders, through: :ordered_drinks 
 end 

(你可能不需要这些关系,就能知道一杯酒的订单量was/is)

然后您编辑 ordered_drinks 而不是 editing/managing drinks 关联,您的 partial/collection-select 将按预期运行。

感谢所有帮助解决这个问题的人,尤其是@nathanvda。 我的错误是忘记了 table 来加入 drink and order,所以我重新创建了项目,除了那个 drink and order table,我还创建了一个 ordereddrink table 就像@nathanvda 回答的那样。

app/models/order.rb

has_many :ordered_drinks, inverse_of: :order 
has_many :feeds, through: :ordered_drinks

accepts_nested_attributes_for :ordered_drinks, allow_destroy: true

app/controllers/orders_controller.rb

before_action :set_drink, only: [:new, :edit, :update, :destroy]
  def set_drink
    @drinks = Drink.all
  end
def order_params
  params.require(:order).permit(:number, ordered_drinks_attributes: [:id, :order_id, :drink_id, :_destroy, drinks_attributes: [:id, :name, :_destroy]])
end

app/views/orders/_form.html.erb

<hr>
<h3>Drinks</h3>
<div id='ordered_drinks'>
  <%= f.fields_for :ordered_drinks do |ordered_drink| %>
    <%= render 'ordered_drink_fields', :f => ordered_drink %>
  <% end %>
  <div class='links'>
    <%= link_to_add_association 'add drink', f, :ordered_drinks %>
  </div>
</div>
<hr>

app/views/orders/_ordered_drink_fields.html.erb

<div class='nested-fields'>
  <div class="field">
    <%= f.collection_select(:drink_id, @drinks, :id, :name, include_blank: false) %>
  </div>
  <%= link_to_remove_association "remove drink", f %>
</div>

如果有遇到同样问题的朋友,可以联系我或者留言。