Help with each method

Ok, I have been messing with this for a while and CANNOT figure how I am
getting this particular error. Here is what I have.

Model

class FlashRate < ActiveRecord::Base
belongs_to :review
has_many :reviews
attr_accessor :questions, :keys
end

Controller

def flash_rate
@flash_rate = FlashRate.new
@review = Review.find(params[:id])
@relation = Relation.find(@review.relation.id)
@flash_questions = FlashRate.new
@flash_rate = FlashRate.new
@flash_rate.questions = @relation.get_questions
@flash_rate.keys = [ :prop1, :prop2, :prop3, :prop4, :prop5, :prop6,
:prop7 ]

end

View

<% @block_title = “Flash Rate” -%>
<% @block_description = “Don’t make others repeat your mistakes” -%>

<%= start_form_tag(:action => “save_flash_rate” , :id => @review ) %>
<% @flash_rate.each do |flash| %>

<%= flash.questions %>
<%= select(:flash_rate, flash_keys , @scale ) %>

<% end %>
<%= submit_tag(" Flash Rate ") %>
<%= end_form_tag %>

When I run this code I get an error I don’t understand from the Action
Controller. It says:
undefined method `each’ for #FlashRate:0x3b22cc0

Any know why I would get this error?

Thanks for the help.

I’m not sure what you’re trying to do (It sounds like you’re trying to
build a quiz game), but I think the error you are getting related to
each has to do with this line in the view:

<% @flash_rate.each do |flash| %>

Your @flash_rate object doesn’t understand ‘each’.

I think you need to refer to one of the following in your view instead:
@flash_rate.reviews,@flash_rate.questions or @flash_rate.keys

That or (I think) you can make flash_rate implement enumerable (by
implementing each) - the book “Ruby for Rails” talks about that, as I’m
sure the Pickaxe book does also.

Hope this helps!!!
Dominique

Controller

def flash_rate
@flash_rate = FlashRate.new
@review = Review.find(params[:id])
@relation = Relation.find(@review.relation.id)
@flash_questions = FlashRate.new
@flash_rate = FlashRate.new
@flash_rate.questions = @relation.get_questions
@flash_rate.keys = [ :prop1, :prop2, :prop3, :prop4, :prop5, :prop6,
:prop7 ]

end

cfinlayson wrote:

Ok, I have been messing with this for a while and CANNOT figure how I am
getting this particular error. Here is what I have.

Model

class FlashRate < ActiveRecord::Base
belongs_to :review
has_many :reviews
attr_accessor :questions, :keys
end

Controller

def flash_rate
@flash_rate = FlashRate.new
@review = Review.find(params[:id])
@relation = Relation.find(@review.relation.id)
@flash_questions = FlashRate.new
@flash_rate = FlashRate.new
@flash_rate.questions = @relation.get_questions
@flash_rate.keys = [ :prop1, :prop2, :prop3, :prop4, :prop5, :prop6,
:prop7 ]

end

View

<% @block_title = “Flash Rate” -%>
<% @block_description = “Don’t make others repeat your mistakes” -%>

<%= start_form_tag(:action => “save_flash_rate” , :id => @review ) %>
<% @flash_rate.each do |flash| %>

<%= flash.questions %>
<%= select(:flash_rate, flash_keys , @scale ) %>

<% end %>
<%= submit_tag(" Flash Rate ") %>
<%= end_form_tag %>

When I run this code I get an error I don’t understand from the Action
Controller. It says:
undefined method `each’ for #FlashRate:0x3b22cc0

Any know why I would get this error?

Thanks for the help.

cfinlayson wrote:

Ok, I have been messing with this for a while and CANNOT figure how I am
getting this particular error. Here is what I have.

Model

class FlashRate < ActiveRecord::Base
belongs_to :review
has_many :reviews
attr_accessor :questions, :keys
end

Controller

def flash_rate
@flash_rate = FlashRate.new
@review = Review.find(params[:id])
@relation = Relation.find(@review.relation.id)
@flash_questions = FlashRate.new
@flash_rate = FlashRate.new
@flash_rate.questions = @relation.get_questions
@flash_rate.keys = [ :prop1, :prop2, :prop3, :prop4, :prop5, :prop6,
:prop7 ]

end

View

<% @block_title = “Flash Rate” -%>
<% @block_description = “Don’t make others repeat your mistakes” -%>

<%= start_form_tag(:action => “save_flash_rate” , :id => @review ) %>
<% @flash_rate.each do |flash| %>

<%= flash.questions %>
<%= select(:flash_rate, flash_keys , @scale ) %>

<% end %>
<%= submit_tag(" Flash Rate ") %>
<%= end_form_tag %>

When I run this code I get an error I don’t understand from the Action
Controller. It says:
undefined method `each’ for #FlashRate:0x3b22cc0

Any know why I would get this error?

Thanks for the help.

I absolutely agree with Dominique. I just finished David Black’s one
week ruby bootcamp (he’s the fellow that wrote “Ruby for Rails” and one
of the best thing I brought away from the class is a new comfort level
with script/console.

By using script/console, you can run an irb session that has access to
all of the models and controllers in your application. Its use un
troubleshooting cannot be stressed enough. You can do things like
flash_rate.respond_to? It is an invaluable tool that will save you hours
of guessing when trying to troubleshoot a problem, and one I urge you to
get comfortable with.