I have been looking at this for some time, with no luck.
I am working with Ruby on Rails. I am attempting to use .each to iterate through an array and use each element to set a variable name. Specifically, the variable will control whether the checkbox on a form is displayed as checked.
I keep recieving this error: `@PG-13_check' is not allowed as an instance variable name It seems like prior problems involving this error revolve around a failure to declare the instance variable with @. But I have included that.
Model:
class Movie < ActiveRecord::Base
attr_accessible, :title, :rating, :description, :release_date
def self.ratings
ratings = [G, PG, PG-13, R]
end
end
Controller Code:
@movies = Movie.all
@all_ratings = Movie.ratings
@all_ratings.each do |x|
instance_variable_set("@#{x}_check", "true")
end
In turn, the view reads:
= check_box_tag "ratings[#{rating}]", 1, checked = @#{rating}_check, :id => "ratings_#{rating}"