简体   繁体   中英

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

I am not experiencing errors locally but am on Heroku. Error is:

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

The 'each' is referring to each i in @position in the <% for i in @positions %> line of my User callback.html.erb view below:

<% 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)

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. 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. 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. 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| version.

Further, testing for nil? rather than just testing for the object itself is almost always redundant and can be eliminated. The nil? test is only relevant when you want to distinguish between false and nil which are the only two non-true values in Ruby. In this case a simple unless (x) is better than 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. 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.

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.

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