ruby - Rails undefined method but method exists -
maybe i've been staring @ code long, i'm doing seems trivial, yet code still isn't working.
i'm using modal submit parameters controller, should patch object, instead, i'm getting undefined method 'ignore_reason=' nil:nilclass
.
the modal
<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 host</h4> </div> <div class="modal-body"> <p>defering host means you're choosing ignore security risks neo has identified given host. host ignored future security score calculations. filling in information below, defer risk host 30 days.</p> <br/> <%= form_for @network_host, url: {controller: "network_hosts", action: "defer_host"} |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>
which submits controllers/network_hosts
method 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
i've restarted rails server after latest migration added ignore_reason
, ignore_name
, ignore_date
, ignore_flag
.
a sample rails console output of networkhost
confirm fields there write to:
#<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>
also, confirm parameters being passed, on rails error page (which shows undefined method error), parameters @ bottom:
parameters: {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"a7kb1fdirzjftrr2hbvt/64z02ufxmztfpzgkjowsfi=", "network_host"=>{"id"=>"76", "ignore_reason"=>"test defer", "ignore_name"=>"jf"}, "commit"=>"save changes"}
any appreciated!
it seems @network_host
in controller nil
cause id
key nested in network_host
(according params).
params[:id] #=> nil networkhost.where(id: params[:id]).first #=> nil params[:network_host][:id] #=> "76"
try:
@network_host = networkhost.where(id: params[:network_host][:id]).first #=> #<networkhost id: 76, location_id: 14, mac_address: "d0:63:b4:00:5b:40", ....>
Comments
Post a Comment