2016-06-07 281 views
1

渲染局部視圖軌3.2 紅寶石2.1.5導致錯誤

我想有一票單形式,其包括多個部分。

其中sectios被稱爲CUSTOMER_INFO

在應用程序/視圖/票/ show.html.slim,我有:

= render 'tickets/sections/customer_info', locals: { customer_info: CustomerInfo.new, ticket: @ticket } 

,並在我的應用程序/視圖/票/節/ _customer_info.html.slim,我有:

= form_for customer_info do |f| 
    - f.hidden_field :ticket_id, :value => ticket.id 
    .form-horizontal-column.customer-info 
    .form-group 
     = f.label :pre_tax_total 
     = f.text_field :pre_tax_total, maxlength: 50 
    .form-group 
     = f.label :post_tax_total 
     = f.text_field :post_tax_total, maxlength: 50 
    .actions = f.submit 'Save' 
    .clear  

當應用程序試圖渲染CUSTOMER_INFO形式,我得到了以下錯誤消息:

undefined method `model_name' for NilClass:Class 

當它擊中形式的第一行:

= form_for customer_info do |f| 

任何想法如何做到這一點?

+0

好像'customer_info'爲零 – usmanali

+0

是的,它是零。當你調用控制器的新動作時,你會得到一個零。我的問題是如何從一個控制器的視圖調用另一個控制器動作的局部視圖。在這種情況下,我處於票據控制器的窗體視圖中,並且我想爲CustomerInfo控制器的新操作呈現一個視圖 – EastsideDeveloper

回答

2

嘗試渲染代碼

= render 'tickets/sections/customer_info', locals: { customer_info: CustomerInfo.new, ticket: @ticket } 

改變

= render partial: 'tickets/sections/customer_info', locals: { customer_info: CustomerInfo.new, ticket: @ticket } 

永遠記得添加partial如果使用locals

+0

我這樣做了,現在我得到了:#<#<類的未定義方法'customer_info_path' :0x000000088fc3e0>:0x007f1f9cef0ca0> – EastsideDeveloper

+0

更改代碼,舊的語法: {:CUSTOMER_INFO => CustomerInfo.new,:票=> @ticket} 並檢查你在哪裏調用模型名稱。 –

+0

我不確定你的意思是「你在哪裏調用model_name」。您建議的語法產生相同的結果 – EastsideDeveloper

0

使用這樣的

= render 'tickets/sections/customer_info', locals: { ticket: @ticket } 

而且

= form_for CustomerInfo.new do |f| 
    - f.hidden_field :ticket_id, :value => ticket.id 
    .form-horizontal-column.customer-info 
    .form-group 
     = f.label :pre_tax_total 
     = f.text_field :pre_tax_total, maxlength: 50 
    .form-group 
     = f.label :post_tax_total 
     = f.text_field :post_tax_total, maxlength: 50 
    .actions = f.submit 'Save' 
    .clear 
+0

這不會改變任何內容。我仍然爲#<#獲取未定義的方法'customer_info_path':0x007f1f9cef0ca0>錯誤 – EastsideDeveloper

+0

現在是不同的錯誤 – Mukesh