也许我一直在盯着这段代码,但是我所做的似乎微不足道,但我的代码仍然没有用。
我正在使用模态向控制器提交参数,该控制器应该修复对象,但我得到的是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">×</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
我在最新迁移后重新启动了我的Rails服务器,该迁移添加了ignore_reason
,ignore_name
,ignore_date
,ignore_flag
。
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>
另外,为了确认参数是否正在传递,在Rails Error页面(显示未定义的方法错误)上,参数位于底部:
Parameters:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"a7kB1fDirzJFtRR2hBVt/64Z02ufxMzTfpZgKJoWSFI=",
"network_host"=>{"id"=>"76",
"ignore_reason"=>"test defer",
"ignore_name"=>"JF"},
"commit"=>"Save changes"}
非常感谢任何帮助!
答案 0 :(得分: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", ....>
答案 1 :(得分:2)
您正在错误地阅读错误消息。问题是该方法未定义 - 问题是您有一个nil
对象。
当您在没有结果的查询上致电.first
时,您会获得nil
;如果你想在对象不存在的情况下抛出异常(提示:你经常这样做),请使用NetworkHost.find(id)
。
答案 2 :(得分:0)
对我来说,它是控制器中强大的参数列表。我忽略了将属性添加到它。因此,当我去创建或编辑对象时,它不会让我。