堆栈级别太深错误 - 这段代码有什么问题?
Stack level too deep error - what's wrong with this code?
我正在使用的控制器中有一些重复的代码,我想将其放入一个方法中。但是,这样做会引发 'stack level too deep' 错误。我明白这意味着某处有一个无限循环,虽然我看不到它(我对此很陌生)!
这是有效的控制器代码,但不是 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
我想我可以把代码改成这样,尽管那是它抛出错误的地方:
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
它在视图中显示为:
<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>
使用这个辅助方法:
def ingredient_matcher(one, two)
(one.fridge.ingredients.pluck(:id) & two.ingredients.pluck(:id)) == two.ingredients.pluck(:id)
end
希望这就是某人能够阐明这一点所需的全部代码 - 我认为这将是我所缺少的非常简单的东西!
提前致谢,史蒂夫。
你在这里造成无限递归:
def category
category_setter(category)
end
这里:
def cuisine
category_setter(cuisine)
end
不传递方法名称,调用您的 setter 方法并传递一个字符串:
category_setter('category')
和
category_setter('cuisine')
我正在使用的控制器中有一些重复的代码,我想将其放入一个方法中。但是,这样做会引发 'stack level too deep' 错误。我明白这意味着某处有一个无限循环,虽然我看不到它(我对此很陌生)!
这是有效的控制器代码,但不是 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
我想我可以把代码改成这样,尽管那是它抛出错误的地方:
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
它在视图中显示为:
<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>
使用这个辅助方法:
def ingredient_matcher(one, two)
(one.fridge.ingredients.pluck(:id) & two.ingredients.pluck(:id)) == two.ingredients.pluck(:id)
end
希望这就是某人能够阐明这一点所需的全部代码 - 我认为这将是我所缺少的非常简单的东西!
提前致谢,史蒂夫。
你在这里造成无限递归:
def category
category_setter(category)
end
这里:
def cuisine
category_setter(cuisine)
end
不传递方法名称,调用您的 setter 方法并传递一个字符串:
category_setter('category')
和
category_setter('cuisine')