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

I am following the Michael Hartle Rails tutorial at the moment, and I managed to reach 7.22 without any major glitches. However, the test result puzzles me:

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 added the following to my users page as indicated in the tutorial:

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

but it still does not work. I tried adding the create method, but that just throws the missing template error ...

In case this helps here, the rake routes team 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, tests that fail are as follows:

  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!

+6
source share
7 answers

Tests should not pass at this point. Follow the tutorial and you will see how it works.

+20
source

I struggled with the same thing for about half an hour before checking the error code and coming right here. @mhartl is obviously correct, and there are no typos in the tutorial, it just gets a little confused when it says that “tests for the registration page” should work again. He did not mention anything about the submissions on the registration page (these should be two tests that are not performed at this point in the textbook).

+4
source

I had the same problem (both registration tests still did not pass at the end of 7.3.1), and later noticed that I made the following error:

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

I hope this helps someone!

+1
source

config.use_transactional_fixtures = true in applications.rb

and make sure you have the create action defined in your usercontroller

0
source

add this to your application / 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 
0
source

You need to add the create action (I had 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 
0
source

There was the same problem. Correction takes place in the “Registration Error” section, p. 305 in the second edition, so just keep going. There is no create-action in the controller. As mentioned above, the mention of working with the test registration page is confusing. Its just a page that works, rather than submitting a form.

The book is absolutely superb, one of the best coding books I've read is actually the best. However, as is often the case with code that spans many pages, or in this case the entire book, it can get a little confusing, especially if you miss something. The solution here was to always clearly state in the book what should work at what stage and what should not, and what things will be done later to make it work. Hartl does, but some spots can still confuse people. Repetition and writing things will not hurt.

0
source

Source: https://habr.com/ru/post/917382/


All Articles