繁体   English   中英

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

[英]Michael Hartl Rails Tutorial chapter 7 error Action not found in UsersController

我现在正在关注迈克尔哈特尔的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

提前感谢任何建议!

测试应该在那时通过。 继续关注本教程,您将看到它是如何工作的。

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

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

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

我希望这对某人有帮助!

application.rb文件中的config.use_transactional_fixtures = true

并确保您在userscontroller中定义了一个创建操作

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM