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
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.
Related
i am a newbie in ruby... I'm trying to convert media into a scorm package using what i found on github but i got an error while trying to run the script in the command prompt undefined method `gsub' for nil:NilClass. I guess it may be due to a defined method. any idea on how i can remove this error ?
dir = ARGV.shift.gsub(/\/+$/, '')
index = nil
media = []
Dir["#{dir}/media/*.json"].each do |file|
id = JSON.parse(File.read(file))
base = file.gsub(/\/media\/.*\.json$/, '')
index = "#{base}/index.html"
name = File.basename file
media.push [name,id]
puts "#{name}: #{id}"
end
As the error says, you are calling the method gsub on an object that is an instance of NilClass, in other words you are calling gsub on nil.
The error message tells you in which method and on which line this happens and how you got to that line of code. You will have to examine the error message to find the place in your code where you are calling gsub on an object that is nil, then you have to examine your code to find out why that object is nil instead of a String as you expect it to.
I am trying to declare a class with a few basic functions in it. The function that seems to be causing a problem has an optional argument that passes a symbol in.
class Bag < RandomizerCollection
def initialize()
end
def select(description:Hash, amt=:all)
end
def empty()
end
end
And the error I am getting is:
Traceback (most recent call last):
1: from test.rb:5:in `<main>'
test.rb:5:in `require_relative': /home/osboxes/Documents/Year4/Design/A1/Bag.rb:9: syntax error, unexpected tIDENTIFIER (SyntaxError)
...ef select(description:hash, amt = :all)
... ^~~
/home/osboxes/Documents/Year4/Design/A1/Bag.rb:9: syntax error, unexpected ')', expecting keyword_end
...t(description:hash, amt = :all)
I'm sure this must be something basic but I just can't figure it out. I am new to Ruby and I found similar questions but none helped me find the issue. Any help is appreciated!
You can't define optional arguments (arg=value) after the definition of the keyword arguments (arg: value).
You can correct it in two ways:
Move optional arg before the keywor arg:
def select(amt=:all, description:Hash)
end
Make the second argument a keyword arg:
def select(description:Hash, amt: :all)
end
Worth reading: https://medium.com/podiihq/ruby-parameters-c178fdcd1f4e
I'm trying to create 2 routes dynamically for the same url but it says
syntax error, unexpected ',', expecting keyword_end (SyntaxError)
for this code:
[:get, :options].each do |x|
send(x), '/my_url' do
# ....
end
end
What am I doing wrong?
The '/my_url' is a parameter of the get function, so it needs to be a parameter of the send function:
send(x, '/my_url') do
# ...
end
I'm going through Ruby Koans, and I"m getting hung up on the test_every_object_has_an_id method in about_objects.rb. The supplied code reads like so:
def test_every_object_has_an_id
obj = Object.new
assert_equal __, obj.object_id.class
end
I know that the answer is Fixnum, but, whenever I run path_to_enlightenment.rb, I get the following error message:
custom_require.rb:36:in `require':about_objects.rb:50: syntax error, unexpected $end, expecting keyword_end (SyntaxError) from rubygems/custom_require.rb:36:in `require'
from path_to_enlightenment.rb:7:in `<main>'
Is there some sort of bug in the code as supplied, or am I doing something wrong?
Missing end Keyword
Somewhere else in your files you have a missing end keyword. Some file is reaching the end of the source file before finding the expected keyword. The error clearly tells you so:
custom_require.rb:36:in `require':about_objects.rb:50: syntax error, unexpected $end, expecting keyword_end (SyntaxError) from rubygems/custom_require.rb:36:in `require' from path_to_enlightenment.rb:7:in `<main>'
Check lines 7, 36, and 50 of the named files. That's the problem you need to fix, and has nothing to do with the fact that:
Object.new.object_id.class
#=> Fixnum
It doesn't ask you which class it is, it ask what Fixnum class is. So the answer must be Object
>> Object.new.object_id.class.is_a? Fixnum
#=> false
>> Object.new.object_id.class.is_a? Object
#=> true
The error is:
C:/Users/Admin/Desktop/MyFirstSelfMadeGame.rb:18: syntax error, unexpected keyword_end, expecting $end
Code:
#!/usr/bin/env ruby
require "gosu"
class GameWindow < Gosu::Window
def initialize(800, 600, false) #Window declaration
super
self.caption("Pokemon")
end
def update
end
def draw
end
def button_down(id)
close if id == Gosu::KbSpace
end
end
GameWindow.new.show
Thanks for answers, i get this problem alot.
No, this is not the whole error, the whole error looks something like:
t.rb:6: syntax error, unexpected tINTEGER, expecting ')'
def initialize(800, 600, false) #Window declaration
^
t.rb:21: syntax error, unexpected kEND, expecting $end
Please note that there are 2 error messages reported by the Ruby interpreter. You have noticed and posted only the 2nd one, but the 1st one is actually the relevant one. In general, if you receive error messages, it's a good rule of thumb to find and fix the 1st one first, because the subsequent ones may be caused by the first one.
You need to specify for function arguments. Incorrect:
def initialize(800, 600, false) #Window declaration
Correct:
def initialize()
super(800, 600, false)
As pts says, you cannot reference objects in a method definition arguments
The method definition arguments should only contain variables that you reference in the method.
If you want to use defaults but have the option to override the arguments when calling the 'new' method you could do...
def initialize(width=800, height=600, full_screen=false)
super(width, height, full_screen)
Also, you should note that self.caption is a getter method and doesn't take arguments
this is wrong
self.caption("Pokemon") # < wrong
this is right
self.caption = "Pokemon" # < right
Take a look at the tutorial game...
https://github.com/jlnr/gosu/wiki/Ruby-Tutorial