A web application is being created with rails.
Like YouTube, I want to create a screen with thumbnails arranged,
I want to call a partial template with render partial:
If i use a variable in the plate, it will be "NameError in Dashboard # index".

Error message

The following error screen is displayed.

Applicable source code


= render 'layouts/hedder'
    = render 'layouts/page_top'
      = image_tag "cover", class: 'cover-img', alt: ""
          % h2.heading popular videos
        % ul.movie-box
          = render partial: 'layouts/movie-box', collection: @chapter


% li.movie-box--list
  = link_to "#" do
    = image_tag "http://img.youtube.com/vi//sddefault.jpg", class: 'movie-box--list--thumb', alt: "# {chapter.title}"
      = chapter.title
    .chapter-pray-time 00:30


class DashboardController

Check if the variable doesn't pass well to the render destination
I tried many things with reference to this article, but the error content did not change, so
I want you to point out!

  • Answer # 1

    Rails guide # Rendering collections


    Variables with names based on partial names are used.

    movie-boxis invalid as a variable name, so I don't know what variable is actually prepared, but
    In such a case, you should specify it explicitly with: as.

    = render partial: 'layouts/movie-box', collection: @chapter, as: 'chapter'

  • Answer # 2

    In error message, chapter is incorrect @chapter It may be.
    Is the chapter used in _movie-box.html.haml the same as @chapter in the calling index? Is it different?
    If it is the same, you can use @chapter.
    If it's different, it's supposed to be crossed, but how was it?