Beginner 问题。 我有表格箱将供应商列入指数。 因此,我想为这一供应商提供新的采购表格(和过滤器@supplier.products)需要通过供应商来做一些.。
Modal 窗口
<% @suppliers.each do |supplier| %>
<li><%= link_to supplier.name, new_purchase_path(@supplier) %></li>
<% end %>
表格
<%= nested_form_for(@purchase) do |f| %>
<div class="field">
<%= f.label :supplier_id, :class => "mandatory" %>
<%= @supplier.name %>
<%= collection_select(:purchase, :supplier_id, Supplier.all, :id, :name, :prompt => true) %>
</div>
我在这样做时发现无错误:NilClass.un Defin methods “name” for nil:NilClass。
无法确定我是否需要在购买新行动代码>时界定<代码>@supplier>。
感谢。
* Edit *
What I m trying to do is create a new form through http://localhost:3004/purchases/new?supplier_id=2
I can verify through the console that supplier_id is set Parameters: {"supplier_id"=>"2"}
however when I submit the form it complains about supplier_id being nil.
How to make sure supplier_id is correctly set?
def new
@supplier = Supplier.find(params[:supplier_id])
@purchase = current_user.purchases.new(params[:supplier_id => @supplier.id])
@products = @supplier.products
@products.each do |product|
@purchase.purchase_items.build(:product_id => product.id)
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: @purchase }
end
end