Domanda

I am following the Rails tutorial and was told to type this in my Static Pages Controller:

def home
    if signed_in?
        @micropost  = current_user.microposts.build
        @feed_items = current_user.feed.paginate(page: params[:page])
    end
end

Here is the result of the RSpec tests:

Failures:

  1) Static Pages Home Page for signed in users should render user's feed
     ←[31mFailure/Error:←[0m ←[31mvisit root_path←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5ae7480>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/static_pages_spec.rb:29:in `block (4 levels) in <top (required)>'←[0m

  2) User
     ←[31mFailure/Error:←[0m ←[31mit { should respond_to(:feed) }←[0m
       ←[31mexpected #<User id: nil, name: "Tim Green", email: "timgreen1@outlook.com", created_at: nil, updated_at: nil, password_digest: "$2a$04$5zT
aqLxQ5E6zT5DU.gGmaeS2qV9ypLOBdwJiXxu8WkjN...", remember_token: nil, admin: false> to respond to :feed←[0m
←[36m     # ./spec/models/user_spec.rb:32:in `block (2 levels) in <top (required)>'←[0m

  3) User micropost associations status feed
     ←[31mFailure/Error:←[0m ←[31mits(:feed) { should_not include(unfollowed_post) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5bb1c80>←[0m
←[36m     # ./spec/models/user_spec.rb:173:in `block (4 levels) in <top (required)>'←[0m

  4) User micropost associations status feed
     ←[31mFailure/Error:←[0m ←[31mits(:feed) { should include(older_micropost) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5bdfdd8>←[0m
←[36m     # ./spec/models/user_spec.rb:172:in `block (4 levels) in <top (required)>'←[0m

  5) User micropost associations status feed
     ←[31mFailure/Error:←[0m ←[31mits(:feed) { should include(newer_micropost) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5c6f718>←[0m
←[36m     # ./spec/models/user_spec.rb:171:in `block (4 levels) in <top (required)>'←[0m

  6) Authentication authorization as wrong user visiting edit page for different user
     ←[31mFailure/Error:←[0m ←[31mbefore { visit edit_user_path(wrong_user) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x6149358>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:110:in `block (5 levels) in <top (required)>'←[0m

  7) Micropost pages micropost creation with invalid information should not create a micropost
     ←[31mFailure/Error:←[0m ←[31mbefore { visit root_path }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x61a3df0>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'←[0m

  8) Micropost pages micropost creation with invalid information error messages
     ←[31mFailure/Error:←[0m ←[31mbefore { visit root_path }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x43aaba0>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'←[0m

  9) Micropost pages micropost creation with valid information should create a micropost
     ←[31mFailure/Error:←[0m ←[31mbefore { visit root_path }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5a73f50>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'←[0m

Finished in 7.74 seconds
←[31m114 examples, 9 failures←[0m

Basically it is returning NoMethodError for the 'feed' method, I understand the error but I can't find where to define this method and how to do it, baring in mind this will return a news feed, any help?

È stato utile?

Soluzione

The errors give you hints. This one:

undefined method `feed' for #<User:0x5ae7480>

shows that you have an instance of the class User and the code is trying to find an instance method named feed on that instance of User.

There's also a failing test that is testing this directly:

expected #<User id: nil, name: "Tim Green", email: "timgreen1@outlook.com", 
created_at: nil, updated_at: nil, password_digest: "$2a$04$5zTaqLxQ5E6zT5DU.gGmaeS2qV9ypLOBdwJiXxu8WkjN...", 
remember_token: nil, admin: false> to respond to :feed

This is in user_spec.rb and is explicitly saying that a User instance should respond to a message (which is another way of saying "method call") named feed.

This means you need to go to your user.rb and add a def feed and implement that method to return what you want it to return.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top