简体   繁体   English

ActionView :: Template :: Error(nil:NilClass的未定义方法“ each”):

[英]ActionView::Template::Error (undefined method `each' for nil:NilClass):

I am not experiencing errors locally but am on Heroku. 我在本地没有遇到错误,但是在Heroku上。 Error is: 错误是:

'ActionView::Template::Error (undefined method `each' for nil:NilClass)' 'ActionView :: Template :: Error(nil:NilClass的未定义方法'each')'

The 'each' is referring to each i in @position in the <% for i in @positions %> line of my User callback.html.erb view below: “每个”是指下面的User callback.html.erb视图的<%for i in @positions%>行中的@position中的每个i:

<% for i in @positions %>
<strong>
<% begin %>
<%= @user.positions.find_by_id(i).title + " at " %>
<% rescue %>
<% end %>

<% begin %>
<%= @user.positions.find_by_id(i).company %>
<% rescue %>
<% end %>
</strong>

Here is the relevant portion of my auth controller (callback section) 这是我的auth控制器的相关部分(回调部分)

def callback

...

    @user = current_user

...

    #positions
    for i in 0..(positions.count-1)

      begin
        @company_i = companies[i]['name']
      rescue
      end

      begin
        @title_i = positions[i]['title']
      rescue
      end

      begin
        @industry_i = companies[i]['industry']
      rescue
      end

      begin
        @start_month_i = positions[i]['start-date']['month']
        @start_year_i = positions[i]['start-date']['year']
      rescue
      end

      begin
        @end_month_i = positions[i]['end-date']['month']
        @end_year_i = positions[i]['end-date']['year']
      rescue
      end

      begin
        @li_pos_id_i = positions[i]['id']
      rescue
      end

      if Position.find_by_li_pos_id(@li_pos_id_i).nil?
        @user.positions.build(li_pos_id: @li_pos_id_i, company: @company_i, title: @title_i, 
          industry: @industry_i, start_month: @start_month_i, start_year: @start_year_i, 
          end_month: @end_month_i, end_year: @end_year_i)
      end
    end

    @user.save
    @positions = @user.positions.map(&:id)
end

I think it has something to do with my .find_by methods returning a nil value, but I'm not sure how to fix it. 我认为这与我的.find_by方法有关,该方法返回nil值,但是我不确定如何解决它。 Thank you! 谢谢!

EDITED AUTH CONTROLLER: 编辑的授权控制者:

positions.each do |position|
      begin
        @li_pos_id = position.id
        @title = position.title
        @company = position.company.name
        @industry = position.company.industry
        @start_month = position.start_date.month
        @start_year = position.start_date.year
        @end_month = position.end_date.month
        @end_year = position.end_date.year
      rescue
      end

      unless Position.find_by_li_pos_id(@li_pos_id)
        current_user.positions.build(li_pos_id: @li_pos_id, title: @title, company: @company, industry: @industry, 
          start_month: @start_month, start_year: @start_year, end_month: @end_month, end_year: @end_year)
      end

      @user.save
      @user.positions.save
    end

What you're doing here is called "Pokemon Exception Handling" since you catch all exceptions and throw them in the trash. 因为您捕获了所有异常并将它们丢弃到垃圾桶中,所以您在这里所做的事情称为“ Pokemon异常处理”。 This is a very bad habit to develop and leads to enormous amounts of frustration if you're working on a team with other developers since it hides unexpected errors and makes debugging significantly harder because you will never get a proper stack trace when in those sections you've walled off. 这是一个非常不好的习惯,如果您与其他开发人员一起工作,则会导致极大的挫败感,因为它隐藏了意外的错误,使调试工作变得非常困难,因为在这些部分中,您将永远无法获得正确的堆栈跟踪围墙了。

Instead of blindly catching exceptions, you should be catching specific, expected exceptions where applicable, and only where they could possibly be raised. 而不是盲目地捕获异常,您应该在适用的情况下并且仅在可能会引发异常的情况下捕获特定的预期异常。 You should also make every effort to avoid generating them in the first place. 您还应该尽一切努力避免首先生成它们。

An example of this would be: 例如:

<% @position_ids.present? and @position_ids.each do |position_id| %>

Calling your position variable i is also very poor style as names of that sort are generally reserved for increments or indexes, nothing more. 调用位置变量i样式也很差,因为此类名称通常保留用于增量或索引,仅此而已。 Using a slightly longer but more descriptive name helps immeasurably. 使用稍长但更具描述性的名称会带来不可估量的帮助。

You're also using this very peculiar for x in y notation for loops where Ruby encourages the use of the more succinct y.each do |x| 您还for x in y循环的for x in y表示法中的for x in y使用了这种非常特殊for x in y方法,其中Ruby鼓励使用更简洁的y.each do |x| version. 版。

Further, testing for nil? 进一步,测试是否nil? rather than just testing for the object itself is almost always redundant and can be eliminated. 而不是仅仅测试对象本身几乎总是多余的,可以消除。 The nil? nil? test is only relevant when you want to distinguish between false and nil which are the only two non-true values in Ruby. 测试仅在您要区分falsenil这是Ruby中仅有的两个非true值)时才有意义。 In this case a simple unless (x) is better than if (x.nil?) . 在这种情况下,简单的unless (x)if (x.nil?)

As to why you're not getting anything in @positions , this is probably because the callback routine is not running in the first place. 至于为什么在@positions没有得到任何东西,这可能是因为callback例程没有首先运行。 From the look of things, unless there's a return somewhere in there, then that value must be assigned at the end of the routine. 从外观上看,除非在那里有return值,否则必须在例程的末尾分配该值。

暂无
暂无

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

相关问题 ActionView::Template::Error(nil:NilClass 的未定义方法“每个”):? - ActionView::Template::Error (undefined method `each' for nil:NilClass):? 评论删除 ActionView::Template::Error (nil:NilClass 的未定义方法 `each'): - Comment delete ActionView::Template::Error (undefined method `each' for nil:NilClass): ActionView :: Template :: Error:未定义的方法&#39;&lt;&#39;为nil:NilClass - ActionView::Template::Error: undefined method `<' for nil:NilClass ActionView :: Template :: Error:ActionView :: Template :: Error:未定义的方法`[]&#39;为nil:NilClass - ActionView::Template::Error: ActionView::Template::Error: undefined method `[]' for nil:NilClass Ruby where子句返回nil对象? ActionView :: Template :: Error(nil:NilClass的未定义方法“ each”): - Ruby where clause is returning nil object? ActionView::Template::Error (undefined method `each' for nil:NilClass): 地理编码器:.nearbys 方法抛出 ActionView::Template::Error(nil:NilClass 的未定义方法 `each&#39;): - Geocoder: .nearbys method throwing ActionView::Template::Error (undefined method `each' for nil:NilClass): 错误ActionView :: Template :: Error(nil:NilClass的未定义方法“名称”) - Error ActionView::Template::Error (undefined method `name' for nil:NilClass) ActionView :: Template :: Error(nil:NilClass的未定义方法“ strip!”) - ActionView::Template::Error (undefined method `strip!' for nil:NilClass) Heroku ActionView :: Template :: Error(nil:NilClass的未定义方法“名称”) - Heroku ActionView::Template::Error (undefined method `name' for nil:NilClass) FactoryGirl Rspec ActionView :: Template :: Error:nil:NilClass的未定义方法 - FactoryGirl Rspec ActionView::Template::Error: undefined method for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM