Orsay Orsay - 2 months ago 10
Ruby Question

undefined method for nil class when rendering partial

I'm trying to run this code and it returns me

undefined method `image' for nil:NilClass

But the syntax and the code seems to be good.

index :

- @recipes.each_slice(4) do |recipes|
- recipes.each do |recipe|
%h4.modal-title= recipe.title
= render :partial =>'show', :locals => {:recipe => @recipe}


= image_tag @recipe.image.url(:medium), class:"recipe_image"


Change your code to:

= render :partial =>'show', :locals => { :recipe => recipe }

Since you don't have a instance variable @recipe, but a local variable recipe defined in the line recipes.each do |recipe|.