【问题标题】:Michael Hartl Rails Tutorial chapter 7 error Action not found in UsersControllerMichael Hartl Rails 教程第 7 章错误 Action not found in UsersController
【发布时间】:2012-06-04 16:00:25
【问题描述】:

我目前正在关注 Michael Hartl 的 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

但它似乎仍然不起作用。我尝试添加一个 create 方法,但这只会引发缺少模板的错误...

如果有帮助,这里是 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

提前感谢您的任何建议!

【问题讨论】:

  • 我看到了 users#create 路由,所以我猜测 create 操作是在您的控制器中定义的?发布适当的测试,可能是某个地方的错字
  • @TheIrishGuy 上面已经发布了失败的测试,谢谢
  • 如果它返回“缺少模板”错误,那么您可能没有create 模板。
  • 如果您正在使用guard/spork,您可能需要停止并启动它们。
  • 我遇到了同样的问题。你在哪里能解决这个问题?

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


【解决方案1】:

测试不应该在那个时候通过。继续按照本教程进行操作,您将了解它是如何工作的。

【讨论】:

  • 确保完全按照@mhartl 的方式执行测试。又名:rspec spec/requests/user_pages_spec.rb -e "signup page" 在本教程中,仅使用 rspec 运行所有测试预计会失败。
【解决方案2】:

在看到错误代码并直接来到这里之前,我在同一件事上挣扎了大约半个小时。 @mhartl 显然是正确的,教程中没有错字,只是当他说“注册页面的测试”应该再次工作时有点令人困惑。他没有提到从注册页面提交的内容(这应该是教程中目前失败的两个测试)。

【讨论】:

    【解决方案3】:

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

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

    我希望这对某人有所帮助!

    【讨论】:

      【解决方案4】:

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

      并确保您在用户控制器中定义了创建操作

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          您需要添加一个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
          

          【讨论】:

            【解决方案7】:

            遇到了同样的问题。修复出现在第二版第 305 页的“注册失败”部分,所以请继续。您缺少控制器中的创建操作。如上所述,注册页面测试工作的提及令人困惑。它只是有效的页面,而不是提交表单。

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多