ruby-on-rails - Michael Hartl Rails Tutorial 第 7 章错误 Action not found in UsersController

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

我目前正在学习 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

我已按照教程的指示将以下内容添加到我的用户 Controller 页面:

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

提前感谢您的任何建议!

最佳答案

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

关于ruby-on-rails - Michael Hartl Rails Tutorial 第 7 章错误 Action not found in UsersController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10884313/

相关文章:

ruby - 在指定命名空间类时,我如何 stub 父类?

ruby - 代码实际上没有在 RSpec 中断言?

html - 在 rails 4.2.4 的布局文件夹中产生的不同标题元素

ruby-on-rails - ZSH `rvm-prompt` on RHS 提示不刷新

ruby-on-rails - 可以使用扣币一次性付款吗?

javascript - 是什么原因导致 datepicker 无法使用 jquery 在 rails 3.1.3 中加载?

ruby-on-rails - 如何从没有直接关联的 link_to 创建资源?

ruby-on-rails - Rails json/jbuilder - 根据类别对 json View 进行分组

ruby - 使用哈希值初始化类

ruby-on-rails - 如何在 update_attributes 设置为 false 的情况下 stub `current_user`?