0

我有发票,在发票索引中我有一个“创建新发票”按钮。这会出现一个表格,他们可以在其中选择发票的客户。但是,我不确定如何将其传递给 invoices#new 操作以在下一页加载之前准备数据。我该怎么做呢?

编辑:

invoices#new 页面之前的表格基本上只是告诉发票它是针对哪个客户的。然后我将在发票新页面中自动填写客户地址

4

1 回答 1

1

在选择客户端操作的控制器中执行以下操作(我将称之为select_client):

def select_client
  @clients = Client.all
end

在您的第一个表单上执行以下操作:

<%= form_tag(new_invoice_path, method: :get %>
# the above can use GET or you could leave it out if you wanted
  <%= select_tag "client", options_from_collection_for_select(@clients, "id", "name") %>
  # button etc.
<% end %>

然后在invoices#new

def new
  # after completing the previous form the invoices#new URL should have the
  # client ID attached to it, http://localhost:3000/invoices/new?client=3
  @client = Client.find(params[:client])
  # the next line should auto-populate your form in invoices/new.html.erb
  @invoice = Invoice.new(address: @client.address, name: @client.name)
end
于 2013-04-02T02:43:37.640 回答