English 中文(简体)
如何通过铁路的表格通过参数
原标题:How to pass parameters through forms with rails

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
问题回答

你有类似名称的变数,令人困惑。

@suppliers @supplier supplier

他们来自哪里?

我最好的猜测是,第一个模式箱应该是:

<% @suppliers.each do |supplier| %>
  <li><%= link_to supplier.name, new_purchase_path(supplier) %></li>
<% end %>

但是,你的错误信息似乎表明供应商是零......这使我怀疑你在填满@suppliers方面所做的工作。

如果没有, A. 尚未接受的供应商 供应商。 所有人也将返回零,而且可能与收集-选择发生坏事(因此,它试图打电话:在有点时,供应商的名称)。 因此,您可能不得不提出如下内容:<条码>。 围绕这种收集......或与你的商业逻辑相适应的其他东西。





相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Elegant way building url request with parameters

There must me a more elegant way to build a URL with parameters in .NET then for example Response.Write("<a href=HeadOfMarketView.aspx"+Session["HOM"] != null ? Session["HOM"]+">Head of Market&...

Can you pass by reference in Java?

Sorry if this sounds like a newbie question, but the other day a Java developer mentioned about passing a paramter by reference (by which it was ment just pass a Reference object) From a C# ...

How to name a method that has an out parameter?

What is the common preference to name a method that has an out parameter inside? Usually I use Get as a prefix to mention that the method returns a value (like GetMyBusiness). But what if there is ...

How to create program startup parameters in python

I m just beginning to learn python and the program I m writing requires parameters for it to run with a specific task. For example (programs name is Samtho) samtho -i Mozilla_Firefox How can I do ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

using parameter in SQL with LIKE keyword

from my C#-programm, I access a SQL Server 2008 Database. I have a table with a fulltextindex and want to search for an indexed entry: SELECT page_id FROM page_categories WHERE page_title LIKE @title ...

采用网路服务方法

我撰写了一个网络服务,期望一个参数(所谓的“hlink”)成为一种ur。 在使用网络服务之前,URLEncode 所涉参数(“hlink”)。 然后我打电话......。

热门标签