2012-06-04 65 views
6

我正在关注Michael Hartl的Rails教程,并且我已经设置为7.22而没有任何主要故障。Michael Hartl Rails教程第7章错误在UsersController中找不到动作

Failures: 

    1) UserPages signup with invalid information should not create a user 
    Failure/Error: expect{click_button submit }.not_to change(User, :count) 
    AbstractController::ActionNotFound: 
     The action 'create' could not be found for UsersController 
    # (eval):2:in `click_button' 
    # ./spec/requests/user_pages_spec.rb:29:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/user_pages_spec.rb:29:in `block (4 levels) in <top (required)>' 

    2) UserPages signup with valid information should create a user 
    Failure/Error: expect{click_button submit}.to change(User, :count).by(1) 
    AbstractController::ActionNotFound: 
     The action 'create' could not be found for UsersController 
    # (eval):2:in `click_button' 
    # ./spec/requests/user_pages_spec.rb:42:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/user_pages_spec.rb:42:in `block (4 levels) in <top (required)>' 

Finished in 0.7718 seconds 
6 examples, 2 failures 

我已经添加了以下到我的用户控制器页面的指示通过教程:

​​

,但它仍然没有按”不过我从测试的输出,说难住了t似乎工作。我试着加入了创建方法,但只是抛出回缺少模板错误...

在情况下,它可以帮助这里的耙路线命令的输出:

~/dev/rails/sample_app$ rake routes 
    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 
    root  /      static_pages#home 
    signup  /signup(.:format)   users#new 
    help  /help(.:format)   static_pages#help 
    about  /about(.:format)   static_pages#about 
    contact  /contact(.:format)  static_pages#contact 

在回应评论,测试失败的是:

describe "signup" do 

     before{ visit signup_path } 
     let(:submit) {"Create my account"} 

     describe "with invalid information" do 
     it "should not create a user" do 
      expect{click_button submit }.not_to change(User, :count) 
     end 
     end 

     describe "with valid information" do 
     before do 
      fill_in "Name", with: "Example User" 
      fill_in "Email", with: "[email protected]" 
      fill_in "Password", with: "foobar" 
      fill_in "Confirmation", with: "foobar" 
     end 

     it "should create a user" do 
      expect{click_button submit}.to change(User, :count).by(1) 
     end 
     end 
    end 

在此先感谢您的任何意见!

+0

我看到用户#创建路由,所以我猜测创建操作是在您的控制器中定义的? 发布适当的测试,可能是错别的地方 – TheIrishGuy

+0

@ TheIrishGuy上面发布了失败的测试,谢谢 – TangoKilo

+0

如果它抛出一个“缺少模板”的错误,那么你可能没有'create'模板。 –

回答

20

测试应当通过在这一点上。继续关注该教程,你会看到它是如何工作的。

+0

请确保您完全按照@mhartl的规定执行测试。 又名:rspec的投机/请求/ user_pages_spec.rb -e“注册页面” 在只用RSpec的预期失败运行所有测试教程这一点。 – vinnymac

0

config.use_transactional_fixtures =在applications.rb文件

真正 ,并确保你已经创建操作在userscontroller定义

1

我有同样的问题(包括注册的测试仍然没有通过在7.3.1结束),后来发现我犯了以下错误:

当我在Users添加create方法控制器,我不小心用create方法覆盖了new方法(我把它们搞糊涂了)。但是,您需要两种方法。现在它可以工作。

我希望这是对某人的帮助!

0

添加到您的应用程序/控制器/ users_controller.rb:

class UsersController < ApplicationController 
    attr_accessible :tags_attributes 
    # GET /users 
    # GET /users.json 
    def index 
    @users = User.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @users } 
    end 
    end 

    # GET /users/1 
    # GET /users/1.json 
    def show 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/new 
    # GET /users/new.json 
    def new 
    @user = User.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/1/edit 
    def edit 
    @user = User.find(params[:id]) 
    end 

    # POST /users 
    # POST /users.json 
    def create 
    @user = User.new(params[:user]) 

    respond_to do |format| 
     if @user.save 

     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render json: @user, status: :created, location: @user } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /users/1 
    # PUT /users/1.json 
    def update 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 
     format.html { redirect_to @user, notice: 'User was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /users/1 
    # DELETE /users/1.json 
    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 

    respond_to do |format| 
     format.html { redirect_to users_url } 
     format.json { head :no_content } 
    end 
    end 
end 
0

您需要添加一个create行动(我有同样的问题):

class UsersController < ApplicationController 
    def show 
    @user = User.find(params[:id]) 
    end 

    def new 
    @user = User.new 
    end 


    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 
end 
4

我与挣扎在拍摄错误代码之前大约半小时也是一样,直接来到这里。 @mhartl显然是正确的,本教程中没有错别字,只是当他说“注册页面的测试”应该再次运行时,这有点令人困惑。他没有提到注册页面提交的内容(应该是本教程目前失败的两个测试)。

0

有同样的问题。解决方法出现在第二版第305页的“注册失败”一节中,所以继续下去。您错过了控制器中的创建操作。如上所述,提到注册页面测试工作是令人困惑的。它只是工作的页面,不提交表单。

这本书绝对精湛,是我读过的最好的编码书籍之一,其实是最好的。但是,经常会遇到许多页面的示例代码,或者整本书的示例代码,它可能会有点混乱,特别是如果您错过了某些内容。这里的解决方案应该总是在书中陈述什么应该在什么阶段工作,什么是没有的,以及稍后将要完成的工作。哈特尔,但有些地方仍然可能会混淆人。重复和拼写的东西不会受到伤害。

相关问题