Orsay Orsay - 1 year ago 106
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"

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download