Ruby on rails MichaelHartl的RubyonRails教程-没有路由操作,但是它在routes.rb中

Ruby on rails MichaelHartl的RubyonRails教程-没有路由操作,但是它在routes.rb中,ruby-on-rails,ruby,Ruby On Rails,Ruby,更新-典型用户错误-伙计们感谢所有的技术帮助!看起来密码重置控制器测试应该从未进行过。出于某种原因-我的控件存在,即使在12.1.1中没有使用测试框架。我很确定我输入了错误的东西&这就是为什么教程没有用户ID的原因。总而言之:解决方案是删除控制器测试-因为稍后会进行集成测试 我有一个错误,声称没有路由,但可以清楚地看到routes.rb文件中的路由&在rake路由中。我正在使用MichaelHartl的RubyonRails教程指南。在第12章,我有一个不及格的测试。我通过移除和添加几个部分来测

更新-典型用户错误-伙计们感谢所有的技术帮助!看起来密码重置控制器测试应该从未进行过。出于某种原因-我的控件存在,即使在12.1.1中没有使用测试框架。我很确定我输入了错误的东西&这就是为什么教程没有用户ID的原因。总而言之:解决方案是删除控制器测试-因为稍后会进行集成测试

我有一个错误,声称没有路由,但可以清楚地看到routes.rb文件中的路由&在rake路由中。我正在使用MichaelHartl的RubyonRails教程指南。在第12章,我有一个不及格的测试。我通过移除和添加几个部分来测试这一点——它看起来实际上在控制器中,尽管它声称没有发现任何操作。我还用有限的知识尽我所知检查了params问题

任何提示都将不胜感激

谢谢

ERROR["test_should_get_edit", PasswordResetsControllerTest, 2016-10-20 15:24:37 +0000]
 test_should_get_edit#PasswordResetsControllerTest (1476977077.08s)
ActionController::UrlGenerationError:         ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"password_resets"}
            test/controllers/password_resets_controller_test.rb:11:in `block in <class:PasswordResetsControllerTest>'
        test/controllers/password_resets_controller_test.rb:11:in `block in <class:PasswordResetsControllerTest>'

  54/54: [=======================================================================] 100% Time: 00:00:03, Time: 00:00:03

Finished in 3.02339s
54 tests, 279 assertions, 0 failures, 1 errors, 0 skips
控制器文件

class PasswordResetsController < ApplicationController
  before_action :get_user,         only: [:edit, :update]
  before_action :valid_user,       only: [:edit, :update]
  before_action :check_expiration, only: [:edit, :update]    # Case (1)

  def new
  end

  def create
    @user = User.find_by(email: params[:password_reset][:email].downcase)
    if @user
      @user.create_reset_digest
      @user.send_password_reset_email
      flash[:info] = "Email sent with password reset instructions"
      redirect_to root_url
    else
      flash.now[:danger] = "Email address not found"
      render 'new'
    end
  end

  def edit
  end

  def update
    if params[:user][:password].empty?
      @user.errors.add(:password, "can't be empty")
      render 'edit'
    elsif @user.update_attributes(user_params)
      log_in @user
      @user.update_attribute(:reset_digest, nil)
      flash[:success] = "Password has been reset."
      redirect_to @user
    else
      render 'edit'
    end
  end


  private

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

    # Before filters

    def get_user
      @user = User.find_by(email: params[:email])
    end

    # Confirms a valid user.
    def valid_user
      unless (@user && @user.activated? &&
              @user.authenticated?(:reset, params[:id]))
        redirect_to root_url
      end
    end

    # Checks expiration of reset token.
    def check_expiration
      if @user.password_reset_expired?
        flash[:danger] = "Password reset has expired."
        redirect_to new_password_reset_url
      end
    end
end
请尝试获取“/password\u resets/new”,而不是get:edit

要检查路由路径,请在终端中运行rake routes并查看
用于:编辑和:新建操作的等效路径。

我认为您应该通过请求传递所需的参数,如下所示:

test "should get edit" do
  get :edit, { id: <id-of-the-resource-for-your-controller> }
  assert_response :success
end

你能包括你运行测试时看到的失败消息吗?是的-对不起-粘贴在上面@Raptroman看起来你没有提供密码重置id请查看你的编辑密码重置route@cema-这是本教程将包含在部分中的内容,还是将在装置中列出的内容?您需要一个用户装置。然后创建一个user int测试:@user=users:michael,并使用:get:edit,{id:@user.reset_token}获取页面。请看我在描述的底部添加了更新。。。从我沮丧地看到。。。创建、新建和编辑都存在于管线中。我放弃了rake routes,因为当我编辑routes.rb文件时,错误消息是不同的,告诉我文件问题超出了我的知识范围,或者位于routes中的实际路由之外。rb+如果您可以使用rspec而不是mini_test,那就太好了。易于学习,您将获得强大的开发人员支持。顺便说一下,michael hartl教程-rails 4默认使用rspec:我尝试了这两个routes.rb&在控制器测试中,我尝试了新建,然后编辑,然后像这样更新。。。通过更新获取“/edit”至“password\u resetsedit”。到目前为止没有变化。我正在使用rails 5教程。。。我想。。。我听到有人说我应该和pry一起去rspec&mini。。。在这种情况下,rspec是更好还是更差?我第一次尝试使用书籍推荐书来解决这个问题……所以我现在不会安装任何额外的gems,我的cloud9帐户允许我在任何时候运行的内存也接近极限@ajay这是解决方案的最佳措辞。正如另外两个人所说,这个错误确实产生了——你的错误帮助我扭转了局面&找到了最好的错误。非常感谢。
class PasswordResetsController < ApplicationController
  before_action :get_user,         only: [:edit, :update]
  before_action :valid_user,       only: [:edit, :update]
  before_action :check_expiration, only: [:edit, :update]    # Case (1)

  def new
  end

  def create
    @user = User.find_by(email: params[:password_reset][:email].downcase)
    if @user
      @user.create_reset_digest
      @user.send_password_reset_email
      flash[:info] = "Email sent with password reset instructions"
      redirect_to root_url
    else
      flash.now[:danger] = "Email address not found"
      render 'new'
    end
  end

  def edit
  end

  def update
    if params[:user][:password].empty?
      @user.errors.add(:password, "can't be empty")
      render 'edit'
    elsif @user.update_attributes(user_params)
      log_in @user
      @user.update_attribute(:reset_digest, nil)
      flash[:success] = "Password has been reset."
      redirect_to @user
    else
      render 'edit'
    end
  end


  private

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

    # Before filters

    def get_user
      @user = User.find_by(email: params[:email])
    end

    # Confirms a valid user.
    def valid_user
      unless (@user && @user.activated? &&
              @user.authenticated?(:reset, params[:id]))
        redirect_to root_url
      end
    end

    # Checks expiration of reset token.
    def check_expiration
      if @user.password_reset_expired?
        flash[:danger] = "Password reset has expired."
        redirect_to new_password_reset_url
      end
    end
end
rake routes
                 Prefix Verb   URI Pattern                             Controller#Action
                   root GET    /                                       static_pages#home
                   help GET    /help(.:format)                         static_pages#help
                  about GET    /about(.:format)                        static_pages#about
                contact GET    /contact(.:format)                      static_pages#contact
                 signup GET    /signup(.:format)                       users#new
                  login GET    /login(.:format)                        sessions#new
                        POST   /login(.:format)                        sessions#create
                 logout DELETE /logout(.:format)                       sessions#destroy
                  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
                        PATCH  /users/:id(.:format)                    users#update
                        PUT    /users/:id(.:format)                    users#update
                        DELETE /users/:id(.:format)                    users#destroy
edit_account_activation GET    /account_activations/:id/edit(.:format) account_activations#edit
        password_resets POST   /password_resets(.:format)              password_resets#create
     new_password_reset GET    /password_resets/new(.:format)          password_resets#new
    edit_password_reset GET    /password_resets/:id/edit(.:format)     password_resets#edit
         password_reset PATCH  /password_resets/:id(.:format)          password_resets#update
                        PUT    /password_resets/:id(.:format)          password_resets#update
             microposts POST   /microposts(.:format)                   microposts#create
              micropost DELETE /microposts/:id(.:format)               microposts#destroy
test "should get edit" do
  get :edit, { id: <id-of-the-resource-for-your-controller> }
  assert_response :success
end