2016-04-21 34 views
0

也許我一直在盯着這段代碼太長,但我在做什麼似乎微不足道,但我的代碼仍然無法正常工作。Rails未定義的方法,但方法存在

我正在使用模式向控制器提交參數,該控制器應該修補對象,但實際上,我得到了undefined method 'ignore_reason=' for nil:NilClass

模態

<div class="modal fade" id="deferHostModal" tabindex="-1" role="dialog" aria-labelledby="deferHostLabel"> 
 
    <div class="modal-dialog" role="document"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title" id="deferHostLabel">Defer a Host</h4> 
 
     </div> 
 
      <div class="modal-body"> 
 
      <p>Defering a host means that you're choosing to ignore the security risks that Neo has identified for the given host. 
 
       This host will be ignored from future security score calculations. By filling in the information below, you will defer risk for the host for 30 days.</p> 
 
      <br/> 
 
      <%= form_for @network_host, url: {controller: "network_hosts", action: "defer_host"} do |f| %> 
 
       <%= f.hidden_field :id, class: "form-control", required: "required" %> 
 
       <%= f.label :ignore_reason, "Defer Reason" %> 
 
       <%= f.text_area :ignore_reason, class: "form-control", required: "required" %> 
 
       <br/> 
 
       <%= f.label :ignore_name, "Your Name" %> 
 
       <%= f.text_field :ignore_name, class: "form-control", required: "required" %> 
 

 
      </div> 
 
      <div class="modal-footer"> 
 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
       <%= f.submit "Save changes", class: "btn btn-primary btn-success" %> 
 
      </div> 
 
     <% end %> 
 
    </div> 
 
    </div> 
 
</div>

它提交給controllers/network_hosts方法defer_host

def defer_host 
 
    @network_host = NetworkHost.where(id: params[:id]).first 
 
    @network_host.ignore_reason = params[:ignore_reason] 
 
    @network_host.ignore_name = params[:ignore_name] 
 
    @network_host.ignore_flag = true 
 
    @network_host.ignore_date = 30.days.from_now 
 
    @network_host.save 
 

 
    redirect_to root_path 
 
    end

我已經重新啓動它添加了ignore_reasonignore_nameignore_dateignore_flag最新的遷移後,我的Rails服務器。

一個NetworkHost的樣品的Rails控制檯輸出,以確認該字段是存在寫入:

#<NetworkHost id: 76, location_id: 14, mac_address: "D0:63:B4:00:5B:40", ip_address: "10.10.10.106", hostname: "OpenELEC", created_at: "2016-02-19 01:03:24", updated_at: "2016-04-14 19:30:21", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_reason: nil, ignore_name: nil, ignore_date: nil, ignore_flag: nil>

此外,爲了確認該參數被傳遞,在軌道錯誤頁面(顯示未定義的方法錯誤),參數在底部:

Parameters: 
 

 
{"utf8"=>"✓", 
 
"_method"=>"patch", 
 
"authenticity_token"=>"a7kB1fDirzJFtRR2hBVt/64Z02ufxMzTfpZgKJoWSFI=", 
 
"network_host"=>{"id"=>"76", 
 
"ignore_reason"=>"test defer", 
 
"ignore_name"=>"JF"}, 
 
"commit"=>"Save changes"}

任何幫助,非常感謝!

回答

3

這似乎是你的@network_host在控制器是nil原因id鍵嵌套在network_host(根據你的參數)。

params[:id] 
#=> nil 
NetworkHost.where(id: params[:id]).first 
#=> nil 
params[:network_host][:id] 
#=> "76" 

嘗試:

@network_host = NetworkHost.where(id: params[:network_host][:id]).first 
#=> #<NetworkHost id: 76, location_id: 14, mac_address: "D0:63:B4:00:5B:40", ....> 
+0

該死的嵌套參數!謝謝@ilya – Godzilla74

+0

@ Godzilla74歡迎您) – Ilya

2

您正在閱讀的錯誤消息是錯誤的。問題不在於該方法未定義 - 問題是您得到了一個nil對象。

當您在沒有結果的查詢中調用.first時,將得到nil;如果你想拋出一個異常如果對象不存在(提示:你最經常這樣做),使用NetworkHost.find(id)

0

對我來說,這是控制器中強大的參數列表。我忽略了將屬性添加到它。所以當我去創建或編輯對象時,它不會讓我。