[英]How do I avoid undefined method `empty?' for nil:NilClass?
I'm running into an error.我遇到了一个错误。
undefined method empty? for nil:NilClass?
It seems to happen part of the time.它似乎在部分时间发生。 I put the same controller code in show and new.
我在 show 和 new 中放置了相同的控制器代码。 Can someone point me in the right direction as to why I'm getting the error and how can this be avoided?
有人可以指出我为什么会收到错误以及如何避免这种错误的正确方向吗? I'm creating customers and attaching a list to them.
我正在创建客户并附上一个列表给他们。 Sometimes the list exists before the customer sometimes not.
有时列表存在于客户之前,有时不存在。 Thanks
谢谢
Setup is Lists has many customers. Setup is Lists 有很多客户。 So I'm going to customers_path and clicking new_customer_path and I get the error.
所以我要去customers_path并单击new_customer_path,我得到了错误。 If I make a list first sometimes it works.
如果我先列一个清单,有时它会起作用。 Sometimes If I refresh things work.
有时如果我刷新事情的工作。 If I reboot the server then it fails on first attempt.
如果我重新启动服务器,那么它在第一次尝试时就会失败。 So I know something is working but I'm not sure why I get inconsistent results?
所以我知道有些东西在起作用,但我不确定为什么我得到不一致的结果? Thoughts
想法
customer_controller.rb - index - Trying to maybe pre set it? customer_controller.rb - 索引 - 试图预先设置它?
@customer = Customer.new
@customers = Customer.where("user_id = ?", uid).order(updated_at: :desc)
# @lists = List.where(user_id: current_user.id).order("created_at asc").pluck(:name, :id)
@lists = List.where(user_id: current_user.id).order("created_at asc")
customer_controller.rb - new customer_controller.rb - 新
def new
@customer = Customer.new
puts " "
puts "============="
# @lists = List.where(user_id: current_user.id).order("created_at asc").pluck(:name, :id) rescue nil
@lists = List.where(user_id: current_user.id).order("created_at asc")
puts @lists.count
puts " "
puts "============="
end
customers_form.erb which is _form.erb customer_form.erb 即 _form.erb
<%= form_for(customer) do |f| %>
<% if customer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(customer.errors.count, "error") %>
prohibited this customer from being saved:</h2>
<ul>
<% ccustomer.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-box">
<div class="field">
<%= f.label "First Name" %>
<%= f.text_field :firstname, :class => 'form-control' %>
</div>
<div class="field center">
<label>List</label>
<%#= f.select :list_id, options_for_select(@lists.map{ |list| [list.name, list.id] }), prompt: 'Not Assigned', class: 'form-control input-lg' %>
<%= f.select :list_id, @lists, { :include_blank => 'Not Assigned'}, class: 'form-control input-lg' %>
</div>
<%= f.hidden_field :user_id, :value => current_user.id %>
<br>
<div class="actions">
<%= f.submit "Save", :class => "btn btn-lg btn-long btn-warning" %>
</div>
</div>
My log output:我的日志输出:
Started GET "/customers/new" for 127.0.0.1 at 2020-03-01 20:37:41 -0800
Processing by CustomersController#new as HTML
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 18], ["LIMIT", 1]]
Rendering customers/new.html.erb within layouts/application
Rendered customers/_form.html.erb (29.6ms)
Rendered customers/new.html.erb within layouts/application (32.3ms)
Completed 500 Internal Server Error in 74ms (ActiveRecord: 11.3ms)
ActionView::Template::Error (undefined method `empty?' for nil:NilClass):
39:
40: <div class="field center">
41: <label>List</label>
42: <%= f.select :list_id, @lists, { :include_blank => 'Not Assigned'}, class: 'form-control input-lg' %>
43: </div>
44: <% end %>
45:
app/views/customers/_form.html.erb:42:in `block in _app_views_customers__form_html_erb__810149171042607559_70218705879220'
app/views/customers/_form.html.erb:1:in `_app_views_customers__form_html_erb__810149171042607559_70218705879220'
app/views/customers/new.html.erb:8:in `_app_views_customers_new_html_erb__187385255323143294_70218705803580'
Rendering /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
Rendering /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (4.0ms)
Rendering /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
Rendering /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
Rendered /Users/MyName/.rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (69.2ms)
Try this, remove pluck
from here试试这个,从这里移除
pluck
@lists = List.where(user_id: current_user.id).order("created_at asc")
map here instead在这里映射
<%= f.select :list_id, options_for_select(@lists.map{ |list| [list.name, list.id] }), prompt: 'Not Assigned', class: 'form-control input-lg' %>
Give it a try!试一试!
A better approach is to start using rescue , below is just a small snippet how to do it -更好的方法是开始使用救援,下面只是一个小片段如何做到这一点 -
def index
begin
@lists = List.where(user_id: current_user.id)
rescue
flash[:notice] = "ERROR"
redirect_to(:action => 'index')
return
end
flash[:notice] = "OK"
redirect_to(:action => 'index')
end
This also works -这也有效 -
@lists = List.where(user_id: current_user.id).order("created_at asc").pluck(:name, :id) rescue nil
Read more about it here - https://guides.rubyonrails.org/debugging_rails_applications.html#catching-exceptions在此处阅读更多相关信息 - https://guides.rubyonrails.org/debugging_rails_applications.html#catch-exceptions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.