0

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"
1
  • The only place you're calling image is in _show, so we know that the problem is @recipe is nil. Please edit your question to include the controller code where you set @recipe. Commented Aug 9, 2016 at 14:35

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.