Orsay
Orsay

Reputation: 1130

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|
  .row
    - recipes.each do |recipe|
      .col-md-3
         %h4.modal-title= recipe.title
         .modal-body
            = render :partial =>'show', :locals => {:recipe => @recipe}

_show:

.main_content
  #recipe_top.row
    .col-md-4
      = image_tag @recipe.image.url(:medium), class:"recipe_image"

Upvotes: 0

Views: 701

Answers (3)

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2072

You have wrong implication in code:

instead of :recipe => @recipe use :recipe => recipe and then in show partial just use recipe instead of @recipe

Or

in index define @recipe = recipe and use @recipe in show.

Upvotes: 1

Navin
Navin

Reputation: 924

Try this, Just replace your instance variable @recipe when passing in locale because you already done eaching on it and gets local variable recipe so pass that in your locale and then call like this in your show page.

 recipe.image.url(:medium)

Upvotes: 1

spickermann
spickermann

Reputation: 106792

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

Upvotes: 2

Related Questions