2012-03-05 31 views
2

我正在編寫一個簡單的窗體來編輯客戶的屬性(使用Rails 3.1和Ruby 1.9.3)。控制器代碼:nil不是form_for的符號

def edit 
    cust_id = params[:cust_id] 
    output_id = params[:output_id] 
    @cust = CustOutput.find(:all, :conditions => {:cust_id => cust_id, :output_id => output_id }) 
    logger.info(@cust[0].cust_id) 
end 

在視圖:

<h2> Editing customer <%[email protected][0].cust_id %> with output id <%[email protected][0].output_id %></h2> 
<div> 
<%= form_for @cust[0] do |cust| %> 
    <%= render "shared/error_messages", :target => @cust[0] %> 
    <div id='id' class='outerDiv'> 
     <%= cust.text_field :cust_id, :size => 20 %> 
    </div> 
    <div id='email1' class='outerDiv'> 
     <label for='email1'>Email Part1</label> 
     <%= cust.text_field :email_part1, :size => 20 %> 
    </div> 
    <div id='email2' class='outerDiv'> 
     <label for='email2'>Email Part 2</label> 
     <%= cust.text_field :email_part2, :size => 20 %> 
    </div> 
    <div id='dt' class='outerDiv'> 
     <label for='dt'>Delivery Type</label> 
     <%= cust.text_field :delivery_type, :size => 20 %> 
    </div> 
    <%= cust.submit "Save" %> 
    <span class="cancel"><%= link_to "Cancel", :action=>"load", :cust_name=>"#{@cust[0].cust_id}" %> 
<% end %> 
</div> 

我從開口編輯頁面得到的錯誤消息(HTTP://本地主機:3000 /編輯的cust_id = 2 & output_id = 6?) :

nil is not a symbol 
Extracted source (around line #3): 

1: <h2> Editing customer <%[email protected][0].cust_id %> with output id <%[email protected][0].output_id %></h2> 
2: <div> 
3: <%= form_for @cust[0] do |cust| %> 
4:  <%= render "shared/error_messages", :target => @cust[0] %> 
5:  <div id='id' class='outerDiv'> 
6:  <%= cust.text_field :cust_id, :size => 20 %> 

首先要檢查@cust [0]是否是一個無對象。在軌控制檯我檢查(使用提供的CUST_ID和output_id)

@cust = CustOutput.find(:all, :conditions => {:cust_id => 2, :output_id => 6 }) 
CustOutput Load (0.7ms) SELECT "cust_outputs".* FROM "cust_outputs" WHERE "cust_outputs"."cust_id" = 22 AND "cust_outputs"."output_id" = 6 
=> [#<custOutput cust_id: 2, output_id: 6, email_part1: "[email protected]", email_part2: nil, delivery_type: "ftp">] 

而且從我把控制器記錄器,實際上是在日誌中的有效和正確的cust_id輸出。那麼,爲什麼我仍然得到零不是一個符號錯誤信息?我也檢查過CustOutput模型,並且所有的attr_accessible字段都存在。

回答

0

如果您只想爲您的收藏中的第一位顧客渲染表格,那麼使用@cust.first怎麼樣?

0

嘗試使用:(單位::first代替:all

@cust = CustOutput.find(:first, :conditions => {:cust_id => cust_id, :output_id => output_id }) 

而在你form_for刪除所有[0]引用

+0

是的,我試過了。它根本不會改變結果。 – swingfuture 2012-03-05 21:16:03

0

這是一個老問題,但這樣做保養時,我最近有同樣的問題一箇舊項目。該項目使用不同的ID列,但主鍵未在模型中指定。

添加這些線上的所有作品後:

self.primary_key = 'uuid' 

項目中使用的Rails 3.1和MySQL 5

相關問題