0

ユーザーがパスワードを変更できるようにするカスタムアクションをDeviseの登録コントローラーに追加しようとしています(registrations#editメール/ユーザー名ではなくパスワードのみを変更するためのフォームが必要なため、デフォルトを使用できません)。以下に記述した実装は開発モードで動作しますが、コントローラーをテストするとこのエラーが発生します。

Failure/Error: get 'password'
ActionController::RoutingError:
  No route matches {:controller=>"registrations", :action=>"password"}

私のコードがあります(無関係な部分をスキップしようとしました)

spec/controllers/registrations_controller_spec.rb

describe RegistrationsController do
  include Devise::TestHelpers

  before :each do
    request.env["devise.mapping"] = Devise.mappings[:users]
  end

  describe "GET 'password'" do
    it "..." do
                     # The problem is here,
      get 'password' # it raises ActionController::RoutingError
    end
  end
end

app/controllers/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  # ...
  def password
  end
end

config/routes.rb

devise_for :users, path: 'account', 
  controllers: { registrations: 'registartions' }, 
  skip: [:registartions, :sessions]

devise_scope :user do
  # ...
  scope '/account' do
    get 'password' => 'devise/registartions#password', as: "change_password
  do
end

spec_helper.rb

# ...
RSpec.configure do |config|
  # ...
  config.include Devise::TestHelpers, :type => :controller
end
4

3 に答える 3

1

config/routes.rbを次のように設定しましたか。

devise_for :users do
    get 'logout' => 'devise/sessions#destroy'
    get 'changepassword' => 'devise/registrations#edit'
    get 'access' => 'homepages#access'
    get 'history' => 'policies#history'
    get 'future' => 'policies#future'
  end
  devise_for :users, :controllers => { :sessions => :sessions }
  resources :users

私も同じ問題を抱えてる。私はルートを設定し、それは私のために働いた:)

于 2012-10-22T11:52:23.140 に答える
1

通常、これにはコメントを追加しますが、コード ブロックを含めると、コメントが乱雑になります。

GETon/passwordの代わりに onを実行しようとしているようです/account/password

私が読んでいるものから、あなたは次のマッピングを持っています/account/password

devise_scope :user do # used to remove /users/ part from devise URLs
  # ...
  scope '/account' do # adds /account to URLs
    get 'password' => 'devise/registartions#password', as: "change_password"  
    # this will match /account/passwordAnswer

  do
end

そのため、スコープを削除するか、テストの get リクエストを次のように置き換える必要があります。

get "/account/password", :user => @user

またはこれ

get change_password_path(@user)

のモック@userはどこにありますか。User

実行rake routesして確認します。

于 2012-10-22T12:03:21.343 に答える