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. 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. So I'm going to customers_path and clicking new_customer_path and I get the error. 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 = 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
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
<%= 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
@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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.