首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby on Rails: test 10.21 on tutorial

Ruby on Rails: test 10.21 on tutorial
EN

Stack Overflow用户
提问于 2016-04-20 07:43:18
回答 1查看 189关注 0票数 0

Test 10.22应该是green,但它是red。我在users_controller中添加了create方法,并删除了这里的另一个create方法。有没有可能是create error或者其他原因导致的错误?

如何解决这个问题?

谢谢

错误和相关文件如下:

代码语言:javascript
复制
ERROR["test_invalid_signup_information", UsersSignupTest, 2016-03-23 16:43:47 +0000]
 test_invalid_signup_information#UsersSignupTest (1458751427.59s)
AbstractController::ActionNotFound:         AbstractController::ActionNotFound: The action 'create' could not be found for UsersController
            test/integration/users_signup_test.rb:8:in `block (2 levels) in <class:UsersSignupTest>'
            test/integration/users_signup_test.rb:7:in `block in <class:UsersSignupTest>'
        test/integration/users_signup_test.rb:8:in `block (2 levels) in <class:UsersSignupTest>'
        test/integration/users_signup_test.rb:7:in `block in <class:UsersSignupTest>'

ERROR["test_valid_signup_information", UsersSignupTest, 2016-03-23 16:43:47 +0000]
 test_valid_signup_information#UsersSignupTest (1458751427.61s)
AbstractController::ActionNotFound:         AbstractController::ActionNotFound: The action 'create' could not be found for UsersController
            test/integration/users_signup_test.rb:21:in `block (2 levels) in <class:UsersSignupTest>'
            test/integration/users_signup_test.rb:20:in `block in <class:UsersSignupTest>'
        test/integration/users_signup_test.rb:21:in `block (2 levels) in <class:UsersSignupTest>'
        test/integration/users_signup_test.rb:20:in `block in <class:UsersSignupTest>'
  38/38: [=====================================================] 100% Time: 00:00:05, Time: 00:00:05

Finished in 5.52461s
38 tests, 153 assertions, 0 failures, 2 errors, 0 skips

和我的代码

代码语言:javascript
复制
# test/integration/users_signup_test.rb
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, user: {name: "",
                              email: "user@invalid",
                              password: "foo",
                              password_confirmation: "bar"}
    end

    assert_template 'users/new'
    assert_select 'div#error_explanation'
    assert_select 'div.field_with_errors'
  end

  test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post_via_redirect users_path, user: {name: "Example User",
                                           email: "user@example.com",
                                           password: "password",
                                           password_confirmation: "password"}
    end
    # assert_template 'users/show'
    # assert is_logged_in?
  end
end

代码语言:javascript
复制
# app/controllers/users_corntroller.rb
class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update]
  before_action :admin_user, only: :destroy

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

  def new
    @user = User.new
  end

  def edit

  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end


  def index
    @users = User.paginate(page: params[:page])
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted"
    redirect_to users_url
  end

  private
  def user_params
    params.require(:user).permit(:name, :email, :password,
                                 :password_confirmation)
  end


  # Before filters

  # Confirms a logged-in user.
  def logged_in_user
    unless logged_in?
      store_location
      flash[:danger] = "Please log in."
      redirect_to login_url
    end
  end

  # Confirms the correct user.
  def correct_user
    @user = User.find(params[:id])
    redirect_to(root_url) unless @user == current_user
  end

  # Confirms an admin user.
  def admin_user

    redirect_to(root_url) unless current_user.admin?
  end

  def create
    @user = User.new(user_params)
    if @user.save
      UserMailer.account_activation(@user).deliver_now
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end
end
EN

回答 1

Stack Overflow用户

发布于 2016-04-20 08:05:32

看起来你在你的控制器的私有部分有了create方法。将create方法移动到控制器的公共部分。我不确定这就是问题所在,但我不是在一台开发计算机上看到我自己。顺便说一下,这是Mike Hartl教程吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36731349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档