2013-05-10 12 views
0

我正在嘗試通過Hartl教程的最後一章,但是在所有規範中不斷收到以下錯誤。只要我將以下/追隨者行爲輸入到我的代碼中,測試就開始失敗。任何人都可以看到我要去哪裏嗎?Hartls第11章:未定義的方法`following_user_path'

ActionView::Template::Error: 
    undefined method `following_user_path' 

下面是失敗一個規範的一個示例:

describe "visiting the following page" do 
    before { visit following_user_path(user) } 
    it { should have_in_title('Sign in') } 
end 

下面是從_stats.html.erb所提取的源(線路#3):

<% @user ||= current_user %> 
<div class="stats"> 
    <a href="<%= following_user_path(@user) %>"> 
     <strong id="following" class="stat"> 
      <%= @user.followed_users.count %> 
     </strong> 
     following 
    </a> 
    <a href="<%= followers_user_path(@user) %>"> 
     <strong id="followers" class="stat"> 
      <%= @user.followers.count %> 
     </strong> 
     followers 
    </a> 
</div> 

routes.rb中

SampleApp::Application.routes.draw do 
    resources :users do 
    member do 
     get :following, :followers 
    end 
    end 
    resources :sessions,  only: [:new, :create, :destroy] 
    resources :microposts, only: [:create, :destroy] 
    resources :relationships, only: [:create, :destroy] 

    root to: 'static_pages#home' 

    match '/signup', to: 'users#new' 
    match '/signin', to: 'sessions#new' 
    match '/signout', to: 'sessions#destroy', via: :delete 

    match '/help', to: 'static_pages#help' 
    match '/about', to: 'static_pages#about' 
    match '/contact', to: 'static_pages#contact' 
end 

users_controller.rb

class UsersController < ApplicationController 
    before_filter :signed_in_user, 
       only: [:index, :edit, :update, :destroy, :following, :followers] 
    before_filter :correct_user, only: [:edit, :update] 
    before_filter :admin_user,  only: :destroy 

    def show 
    @user = User.find(params[:id]) 
    @microposts = @user.microposts.paginate(page: params[:page]) 
    end 

    def new 
    if signed_in? 
     redirect_to root_path, notice: "Already signed in." 
    else 
     @user = User.new 
    end 
    end 

    def create 
    if signed_in? 
     redirect_to root_path, notice: "Already signed in." 
    else 
     @user = User.new(params[:user]) 
     if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
     else 
     render 'new' 
     end 
    end 
    end 

    def edit 
    end 

    def update 
    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 

    def destroy 
    user = User.find(params[:id]) 
    if (current_user == user) && current_user.admin? 
     flash[:error] = "Cannot delete own admin account." 
    else 
     user.destroy 
     flash[:success] = "User destroyed." 
    end 
    redirect_to users_url 
    end 

    def following 
    @title = "Following" 
    @user = User.find(params[:id]) 
    @users = @user.followed_users.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    def followers 
    @title = "Followers" 
    @user = User.find(params[:id]) 
    @users = @user.followers.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    private 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_path) unless current_user?(@user) 
    end 

    def admin_user 
     redirect_to(root_path) unless current_user.admin? 
    end 
end 

user.rb

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation 
    has_secure_password 
    has_many :microposts, dependent: :destroy 
    has_many :relationships, foreign_key: "follower_id", dependent: :destroy 
    has_many :followed_users, through: :relationships, source: :followed 
    has_many :reverse_relationships, foreign_key: "followed_id", 
            class_name: "Relationship", 
            dependent: :destroy 
    has_many :followers, through: :reverse_relationships, source: :follower 

    before_save { |user| user.email = email.downcase } 
    before_save :create_remember_token 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    validates :password, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

    def feed 
    microposts 
    #Micropost.where("user_id = ?", id) 
    end 

    def following?(other_user) 
    relationships.find_by_followed_id(other_user.id) 
    end 

    def follow!(other_user) 
    relationships.create!(followed_id: other_user.id) 
    end 

    def unfollow!(other_user) 
    relationships.find_by_followed_id(other_user.id).destroy 
    end 

    private 

    def create_remember_token 
     self.remember_token = SecureRandom.urlsafe_base64 
    end 
end 

耙路線

following_user GET /users/:id/following(.:format) users#following 
followers_user GET /users/:id/followers(.:format) users#followers 
     users GET /users(.:format)    users#index 
       POST /users(.:format)    users#create 
     new_user GET /users/new(.:format)   users#new 
    edit_user GET /users/:id/edit(.:format)  users#edit 
      user GET /users/:id(.:format)   users#show 
       PUT /users/:id(.:format)   users#update 
       DELETE /users/:id(.:format)   users#destroy 
     sessions POST /sessions(.:format)   sessions#create 
    new_session GET /sessions/new(.:format)  sessions#new 
     session DELETE /sessions/:id(.:format)  sessions#destroy 
    microposts POST /microposts(.:format)   microposts#create 
    micropost DELETE /microposts/:id(.:format)  microposts#destroy 
relationships POST /relationships(.:format)  relationships#create 
    relationship DELETE /relationships/:id(.:format) relationships#destroy 
      root  /       static_pages#home 
     signup  /signup(.:format)    users#new 
     signin  /signin(.:format)    sessions#new 
     signout DELETE /signout(.:format)    sessions#destroy 
      help  /help(.:format)    static_pages#help 
     about  /about(.:format)    static_pages#about 
     contact  /contact(.:format)    static_pages#contact 

spec_helper.rb

require 'rubygems' 
require 'spork' 

Spork.prefork do 
    ENV["RAILS_ENV"] ||= 'test' 
    require File.expand_path("../../config/environment", __FILE__) 
    require 'rspec/rails' 
    require 'rspec/autorun' 

    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

    RSpec.configure do |config| 
    config.mock_with :rspec 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.use_transactional_fixtures = true 
    config.infer_base_class_for_anonymous_controllers = false 
    end 
end 

Spork.each_run do 
end 

ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

RSpec.configure do |config| 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.use_transactional_fixtures = true 
    config.infer_base_class_for_anonymous_controllers = false 
    config.order = "random" 
    config.include Rails.application.routes.url_helpers 
end 

而且,這是我第一次在這裏發帖,所以如果原諒我我沒有正確發佈內容。如果還有其他有用的信息,請告訴我。謝謝!

+0

當你在終端中鍵入'rake routes'時,你能產生結果嗎? – jason328 2013-05-10 19:11:57

+0

剛剛添加 - 謝謝,jason328! – user2371191 2013-05-10 19:14:10

回答

0

不幸的是,不能在測試中調用路由方法。你可以添加它。請檢查您的spec_helper.rb文件中是否有以下代碼。

Rspec.configure do |config| 
    # The line below should be in the file, if not add it. 
    config.include Rails.application.routes.url_helpers 
end 

編輯:您spec_helper.rb文件應該是這樣的

require 'rubygems' 
require 'spork' 

Spork.prefork do 
    ENV["RAILS_ENV"] ||= 'test' 
    require File.expand_path("../../config/environment", __FILE__) 
    require 'rspec/rails' 
    require 'rspec/autorun' 

    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

    RSpec.configure do |config| 
    config.mock_with :rspec 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.use_transactional_fixtures = true 
    config.infer_base_class_for_anonymous_controllers = false 
    config.include Rails.application.routes.url_helpers 
    end 
end 

Spork.each_run do 
end 

再次編輯:叉勺是用於運行測試一個漂亮的小工具。長話短說,如果你有很多測試,它需要一段時間。 Spork通過使測試更快速來幫助緩解這種情況。一旦你對測試更加熟悉,我建議你看看它。

+0

在我的spec_helper.rb文件中沒有看到(應該有單獨的rspec_helper.rb文件?)。加入後,仍然有相同的錯誤。發佈spec_helper.rb以上 – user2371191 2013-05-10 19:21:27

+0

你在測試中使用spork嗎?對不起,我的意思是'spec_helper.rb'。 – jason328 2013-05-10 19:27:31

+0

這就是我的新事物將使事情變得艱難的地方。我在Hartl教程中包含基於早期recod的spork。我實際上並不確定如何使用它(或者它爲此做了什麼): -/ – user2371191 2013-05-10 19:32:36