Michael Hartl Rails教程第7章错误在UsersController中找不到操作

时间:2012-06-04 16:00:26

标签: ruby-on-rails ruby rspec railstutorial.org

我正在关注迈克尔·哈特尔的Rails教程,我已经成功达到了7.22而没有任何重大障碍。但是我对测试的结果感到困惑:

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

我已按照教程的说明将以下内容添加到我的用户控制器页面中:

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

  def new
    @user = User.new
  end

end

但它似乎仍然不起作用。我已经尝试添加一个创建方法,但这只会丢失一个丢失的模板错误......

如果它有帮助,这里是rake routes命令的输出:

~/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: "user@example.com"
          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

提前感谢任何建议!

7 个答案:

答案 0 :(得分:20)

测试不应该在那时通过。继续学习本教程,你会看到它是如何工作的。

答案 1 :(得分:4)

我在同样的事情上挣扎了大约半小时,然后才瞥了一眼错误代码并直接来到这里。 @mhartl显然是正确的,并且教程中没有拼写错误,只是当他说“注册页面的测试”应该再次起作用时,它有点令人困惑。他没有提到注册页面提交的内容(这应该是教程中此时失败的两个测试)。

答案 2 :(得分:1)

我遇到了同样的问题(两次注册测试仍未在7.3.1结束时通过)后来发现我犯了以下错误:

当我在create控制器中添加Users方法时,我意外地用new方法覆盖了create方法(我把它们弄糊涂了)。但是,您需要两种方法。现在它有效。

我希望这对某人有帮助!

答案 3 :(得分:0)

config.use_transactional_fixtures = true 在您的applications.rb文件中

并确保您在userscontroller

中定义了创建操作

答案 4 :(得分:0)

将此添加到您的app / controller / 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

答案 5 :(得分: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

答案 6 :(得分:0)

有同样的问题。修复来自&#34;注册失败&#34;部分,第305页第二版,所以继续。您缺少控制器中的create-action。如上所述,注册页面测试工作的提及令人困惑。它只是工作的页面,而不是提交表单。

这本书绝对精湛,是我读过的最好的编码书之一,实际上是最好的。然而,正如经常使用跨越许多页面的示例代码或在本例中整本书一样,它可能会有点混乱,特别是如果你错过了什么。这里的解决方案是始终在书中非常清楚地说明什么应该在什么阶段工作,什么不存在以及后来要做些什么事情才能让工作起作用。哈特尔确实如此,但有些地方仍有可能让人感到困惑。重复和拼写出来的东西不会受到伤害。