繁体   English   中英

如何避免未定义的方法“空”? 对于零:NilClass?

[英]How do I avoid undefined method `empty?' for nil:NilClass?

我遇到了一个错误。 undefined method empty? for nil:NilClass? 它似乎在部分时间发生。 我在 show 和 new 中放置了相同的控制器代码。 有人可以指出我为什么会收到错误以及如何避免这种错误的正确方向吗? 我正在创建客户并附上一个列表给他们。 有时列表存在于客户之前,有时不存在。 谢谢

Setup is Lists 有很多客户。 所以我要去customers_path并单击new_customer_path,我得到了错误。 如果我先列一个清单,有时它会起作用。 有时如果我刷新事情的工作。 如果我重新启动服务器,那么它在第一次尝试时就会失败。 所以我知道有些东西在起作用,但我不确定为什么我得到不一致的结果? 想法

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 - 新

  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

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>

我的日志输出:

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)

试试这个,从这里移除pluck

@lists = List.where(user_id: current_user.id).order("created_at asc")

在这里映射

<%= f.select :list_id, options_for_select(@lists.map{ |list| [list.name, list.id] }), prompt: 'Not Assigned', class: 'form-control input-lg' %>

试一试!

更好的方法是开始使用救援,下面只是一个小片段如何做到这一点 -

    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

这也有效 -

  @lists = List.where(user_id: current_user.id).order("created_at asc").pluck(:name, :id) rescue nil

在此处阅读更多相关信息 - https://guides.rubyonrails.org/debugging_rails_applications.html#catch-exceptions

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM