当我尝试测试goals控制器时,出现了ActionController :: UrlGenerationError:No route matches(:action =>“edit”,:controller =>“goals”)错误。
这是我的goals_controller_test.rb文件:
require 'test_helper'
class GoalsControllerTest < ActionController::TestCase
test "should be redirected when not logged in" do
get :new
assert_response :redirect
assert_redirected_to new_user_session_path
end
test "should render the new page when logged in" do
sign_in users(:guillermo)
get :new
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
end
这是我的 routes.rb 文件。
Rails.application.routes.draw do
devise_for :users
authenticated :user do
root 'du#dashboard', as: "authenticated_root"
end
resources :goals
root 'du#Home'
end
我的 goals_controller.rb
class GoalsController < ApplicationController
before_filter :authenticate_user!, only: [:new]
def new
end
def edit
end
def show
end
private
def find_user
@user = User.find(params[:user_id])
end
def find_goal
@goal = Goal.find(params[:id])
end
end
我觉得很奇怪,如果我使用 get 'goals/edit' 而不是 resources :goals 测试就能通过。
非常感谢您的任何指导。