2016-04-28 85 views
0

我在ROR中完全陌生並嘗試構建應用程序。 我嘗試在合作伙伴之外創建(並在該視圖之後)客戶。我需要首先打開客戶表單,因爲在最終創建之前它需要額外的信息。嘗試從其他項目通過button_to創建項目new_item_path

夥伴形式,show.html.erb:

<%= button_to 'Create Customer', 
new_customer_path(partner_id: @partner.id), :method => :get %> 

客戶控制器

def new 
    # @customer = Customer.new 
    partner = Partner.find(params[:partner_id]) 
    @customer = Customer.new(partner_id: @partner.id) 
end 

在客戶表中的partner_id字段的定義(我不知道關於其中之一),發生事實後,做了一個add_column

class AddPartnerIdtoCustomers < ActiveRecord::Migration 
    def change 
    add_column :customers, :partner_id, :integer, references: :partners 

    add_index :customers, :partner_id 
    end 
end 

我一直試圖改變所有的時間,並不斷收到錯誤!與我寫在這裏,我得到的錯誤:

Couldn't find Partner without an ID 

有人可以幫忙嗎?並解釋什麼和爲什麼會出錯? 謝謝!!!!

+0

什麼是爲該行動生成的'params'? – Pavan

回答

0

只需將button_to更改爲link_to即可。

<%= link_to 'Create Customer', 
new_customer_path(partner_id: @partner.id), class: 'btn btn-default' %> 

button_to生成一個表單,並將其作爲空的參數發送。

<form class="button_to" method="get" action="/categories/new?plan=1"> 
    <input type="submit" value="Create Customer"> 
</form> 

link_to用params生成實際的錨標籤。

<a href="/categories/new?plan=1">Create Customer</a>