rubyhaml

HAML unexptected instance variable


I'm trying to render an index haml view with bootstrap and variables that are defined in the controller.

playlists.controller.rb

# frozen_string_literal: true

class PlaylistsController < ApplicationController
  def index
    @playlists = Playlist.all
  end

  # Redirect to the First Screen in the Playlist
  # Use this to setup a Playlist Screen !!
  def show
    playlist = Playlist.find(params[:id])
    playlist_screen = playlist.playlist_screens.first

    redirect_to playlist_screen_path(playlist, playlist_screen.screen)
  end
end

index.haml

.row
  - playlist in @playlists.each do |playlist|
  .col-lg-2
    .card
      = link_to playlist.name, playlist

However it seems like it doesn't recognize the variables somehow and I don't understand what's wrong here

error

playlist in @playlists.each do |playlist|
             ^~~~~~~~~~

/home/uralbuntu/Documents/VS/Ruby/screen-rotator/app/views/playlists/index.haml:2: syntax error, unexpected instance variable
 playlist in @playlists.each do |playlist|
             ^~~~~~~~~~
/home/uralbuntu/Documents/VS/Ruby/screen-rotator/app/views/playlists/index.haml:2: syntax error, unexpected instance variable
 playlist in @playlists.each do |playlist|
             ^~~~~~~~~~
/home/uralbuntu/Documents/VS/Ruby/screen-rotator/app/views/playlists/index.haml:2: syntax error, unexpected instance variable
 playlist in @playlists.each do |playlist|
             ^~~~~~~~~~
/home/uralbuntu/Documents/VS/Ruby/screen-rotator/app/views/playlists/index.haml:2: syntax error, unexpected instance variable
 playlist in @playlists.each do |playlist|
             ^~~~~~~~~~
/home/uralbuntu/Documents/VS/Ruby/screen-rotator/app/views/playlists/index.haml:2: syntax error, unexpected instance variable
 playlist in @playlists.each do |playlist|
             ^~~~~~~~~~
/home/uralbuntu/Documents/VS/Ruby/screen-rotator/app/views/playlists/index.haml:2: syntax error, unexpected instance variable
 playlist in @playlists.each do |playlist|
             ^~~~~~~~~~

I also tried without bootstrap, but it gives me the same error


Solution

  • The error is not about a variable that cannot be found. It is a syntax error in the code.

    Just change this line (which is not valid Ruby)

    - playlist in @playlists.each do |playlist|
    

    into

    - @playlists.each do |playlist|
    

    to fix the Ruby syntax error.