undefined method 'each'


J

Joshua Ball

I am following along in Ben Scofield's book (Practical REST on Rails 2 Proj=
ects). As I was building the MovieList application, I ran into an undefined=
method for each, which I can't figure out how to debug (I am new to ruby/r=
ails).

Can someone point me in the correct direction on how to debug this issue?



NoMethodError in MoviesController#update

undefined method `each' for #<Role:0x56ea30c>

RAILS_ROOT: E:/study/rails/movielist

Application Trace<http://localhost:3000/movies/1> | Framework Trace<http://=
localhost:3000/movies/1> | Full Trace<http://localhost:3000/movies/1>

E:/bin/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/=
lib/active_record/attribute_methods.rb:256:in `method_missing'

app/models/movie.rb:17:in `deleted_roles=3D'

app/controllers/movies_controller.rb:70:in `update'

app/controllers/movies_controller.rb:69:in `update'



movies_controller.rb:

65 def update

66 @movie =3D Movie.find(params[:id])

67 debugger

68

69 respond_to do |format|

70 if @movie.update_attributes(params[:movie])

71 flash[:notice] =3D 'Movie was successfully updated.'

72 format.html { redirect_to(@movie) }

73 format.xml { head :eek:k }

74 else

75 format.html { render :action =3D> "edit" }

76 format.xml { render :xml =3D> @movie.errors, :status =3D> :unpro=
cessable_entity }

77 end

78 end

79 end



Movies.rb:

16 def deleted_roles=3D(values)

17 roles.find(*values).each(&:destroy)

18 end





Role.rb:

class Role < ActiveRecord::Base
belongs_to :movie
belongs_to :person

validates_presence_of :movie_id, :person_id, :name

def to_s
[person.full_name, name].join(' - ')
end
end
 
Ad

Advertisements

B

Bill Walton

Hi Joshua,

Joshua said:
NoMethodError in MoviesController#update

undefined method `each' for #<Role:0x56ea30c>

You should post this question to the Rails list at
(e-mail address removed) When you do, you'll need to provide some
additional info.

Best regards,
Bill
 
Ad

Advertisements

J

James Herdman

[Note: parts of this message were removed to make it a legal post.]

Hi.

Note that #deleted_roles= is a setter method (you can tell by the equality
sign) and that in the update method you are assigning to this method.

You probably need to look into attr_accessible to make a whitelist of all
setters you want to be mass-assignable during #update and #create on your
controller (a mass-assignable attribute is one that you can safely assign
values to without worry -- an example of something you wouldn't want to mass
assign is an the primary key value).

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top