Stack level too deep error - what's wrong with this code?

SRack

I've a bit of repeated code in a controller I'm using, which I thought I'd drop into a method. However, doing so raises a 'stack level too deep' error. I understand this means there's an infinite loop in there somewhere, though I can't see it (I'm pretty new to this)!

Here's the controller code that works, though isn't DRY:

def cuisine
    if params[:cuisine].present?
      @recipes = Recipe.all.includes(:cuisines).where(cuisines: { name: params[:cuisine].humanize })
    end
    @message = "There are no recipes matching this cuisine. Please return to" if @recipes.empty?
    @user = current_user
    @count = @recipes.count
  end

  def category
    if params[:category].present?
      @recipes = Recipe.all.includes(:categories).where(categories: { name: params[:category].humanize })
    end
    @message = "There are no recipes matching this category. Please return to" if @recipes.empty?
    @user = current_user
    @count = @recipes.count
  end

I thought I could change the code to this, though that's where it's throwing up the error:

def category
  category_setter(category)
end

def cuisine
  category_setter(cuisine)
end

private

    def category_setter(c)
      @user = current_user
      @count = @recipes.count
      sym = c.to_sym
      syms = c.pluralize.to_sym
      if params[sym].present?
        @recipes = Recipe.all.includes(syms).where(syms: { name: params[sym].to_s.humanize })
      end
      @message = "There are no recipes matching this #{sym}. Please return to" if @recipes.empty?
    end

It's displayed as such in the view:

<div>
      <% @recipes.each do |recipe| %>
        <% if ingredient_matcher(@user, recipe)  %>
          </br><strong><%= recipe.name %></strong>
              <ul>
                <% recipe.ingredients.each do |ing| %>
                  <li><%= ing.name.humanize %></li>
                <% end %>
              </ul>
              <p><%= recipe.method %></p>
        <% else %>
          <% @count -= 1  %>
        <% end %> 
      <% end %>
</div>

Using this helper method:

def ingredient_matcher(one, two)
    (one.fridge.ingredients.pluck(:id) & two.ingredients.pluck(:id)) == two.ingredients.pluck(:id)
  end

Hope that's all the code needed for someone to be able to shed some light on to this - I'm thinking it'll be something pretty straightforward I'm missing!

Thanks in advance, Steve.

dgilperez

You are causing infinite recursion here:

def category
  category_setter(category)
end

and here:

def cuisine
  category_setter(cuisine)
end

Instead of passing the method name, call your setter method passing it a String:

category_setter('category')

And

category_setter('cuisine')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 4 Stack level too deep Error

From Dev

Stack level too deep

From Dev

Stack level too deep

From Dev

upgrading 3.12 to 4, run rails s, get stack level too deep error in local_cache_middleware

From Dev

upgrading 3.12 to 4, run rails s, get stack level too deep error in local_cache_middleware

From Dev

SystemStackError - Stack Level Too Deep

From Dev

Stack level too deep with Devise

From Dev

Stack Level Too Deep error - produced with strong parameters I think

From Dev

Stack Level Too Deep Error on Calling Yield in Ruby

From Dev

Merging Ranges using Sets - Error - Stack level too deep (SystemStackError)

From Dev

ruby on rails. stack level too deep error

From Dev

Rails 3.2.21 instrumenter stack level too deep

From Dev

alias_method: stack level too deep

From Dev

Stack level too deep and before_save

From Dev

Ruby, stack level too deep (SystemStackError)

From Dev

Stack level too deep because recursion

From Dev

rspec + factorygirl : stack level too deep

From Dev

Stack level too deep on user.save

From Dev

Stack Level Too Deep, Modules and Classes

From Dev

SystemStackError - stack level too deep with a User Search

From Dev

Stack level too deep on sequel eager load

From Dev

Ruby : stack level too deep (SystemStackError)

From Dev

Each loop, stack level too deep (SystemStackError)

From Dev

SystemStackError (stack level too deep) in Model

From Dev

Ruby routes - stack level too deep

From Dev

Stack level too deep in Ruby in subscription model

From Dev

Rails 5.1.6 - Stack level too deep

From Dev

Merge Sort confusion, stack level too deep?

From Dev

Naming array method 'to_hash' causes 'stack level too deep' error

Related Related

HotTag

Archive