简体   繁体   中英

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

I'm following Michael Hartl's Rails Tutorial at the moment and I've managed to 7.22 without any major hitches. However I'm stumped by the output from the testing which says:

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

I've added the following to my users controllers page as instructed by the tutorial:

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

  def new
    @user = User.new
  end

end

but it still doesn't seem to work. I've tried adding a create method but that just throws back a missing template error...

In case it helps here's the output of the rake routes command:

~/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

In response to a comment, the tests which are failing are:

   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

Thanks in advance for any advice!

The tests shouldn't be passing at that point. Keep following the tutorial and you'll see how it works.

I struggled with the same thing for about a half hour before goggling the error code and coming directly here. @mhartl is obviously correct and there is no typo in the tutorial, it is just that it is a little confusing when he says that the "tests for the signup page" should be working again. He mentioned nothing about submissions from the signup page (which should be the two tests that are failing at this point in the tutorial).

I had the same problem (both signup-tests were still not passing at the end of 7.3.1) and later noticed that I had made the following mistake:

When I added the create method in the Users controller, I accidentally overwrote the new method with the create method (I confused them). However, you need both methods. Now it works.

I hope this is of help for someone!

config.use_transactional_fixtures = true in your applications.rb file

and make sure you have a create action defined in your userscontroller

add this to your 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

You need to add a create action (I was having the same problem):

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

Had the same problem. Fix comes in the "Sign-up failure" section, page 305 in second edition, so just keep going. You are missing the create-action in controller. As said above, the mention of sign-up page test working is confusing. Its just the page that works, not submitting the form.

The book is absolutely superb, one of the best coding books I have read, in fact the best. However, as often with sample code spanning many pages or in this case the whole book, it can get a bit confusing, especially if you miss something. The solution here would be to always state very clearly in the book what is supposed to work at what stage and what isnt and which things are going to be done later to get stuff to work. Hartl does, but some spots are still likely to confuse people. Repetition and spelling things out would not hurt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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