设计重置密码未知格式

devise reset password unknown format

我使用这个装置 simple_token_authentication。查询的基本格式 - json。 我的 routes.rb:

Rails.application.routes.draw do
  devise_for :users, controllers: {
        registrations: 'users/registrations',
        passwords: 'users/passwords'
  }
  devise_scope :user do
    post "users/reset_password" => "users/registrations#reset_password", as: 'reset_password', defaults: { format: :json }
  end
  resources :cities, :institution_types, defaults: { format: :json } do
    resources :institutions, only: [:index, :new, :create]
  end
  resources :institutions, only: [:show, :edit, :update, :destroy]
  root 'persons#profile'
end

响应请求 localhost:3000/users/reset_password。json 一切正常,收到带有 link 的电子邮件消息。当您单击 link 时,将打开默认视图 devise/passwords/edit。我输入新密码,点击提交,出现错误:

ActionController::UnknownFormat in Users::PasswordsController#update

但在控制台中我看到密码已成功更改。

Started PUT "/users/password" for ::1 at 2016-03-13 16:40:40 +0500
Processing by Users::PasswordsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MZVw3cwbkW+qgySSzKJLyfvNI0ZgKrG1AyTAYAQsIlQAK7mYyxoGlnlemjmajcBZz/zhL3Krmr7fhiNUqtahcQ==", "user"=>{"reset_password_token"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Change my password"}
  User Load (1.1ms)  SELECT  "users".* FROM "users" WHERE "users"."reset_password_token" =   ORDER BY "users"."id" ASC LIMIT 1  [["reset_password_token", "0733ad150b4a7d695e5125a79271f96fd7fa140d195480dcb215ecc03c048c6c"]]
   (0.2ms)  BEGIN
  SQL (0.6ms)  UPDATE "users" SET "encrypted_password" = , "updated_at" = , "reset_password_token" = , "reset_password_sent_at" =  WHERE "users"."id" =   [["encrypted_password", "auUu/Rv/c01HMxvHfDhdlunlNGejtXyVRtUQwEf2SpYOe0qlRIdlC"], ["updated_at", "2016-03-13 11:40:40.272568"], ["reset_password_token", nil], ["reset_password_sent_at", nil], ["id", 3]]
   (12.4ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.9ms)  UPDATE "users" SET "last_sign_in_at" = , "current_sign_in_at" = , "sign_in_count" = , "updated_at" =  WHERE "users"."id" =   [["last_sign_in_at", "2016-03-13 11:39:10.385224"], ["current_sign_in_at", "2016-03-13 11:40:40.292489"], ["sign_in_count", 5], ["updated_at", "2016-03-13 11:40:40.295309"], ["id", 3]]
   (6.4ms)  COMMIT
Completed 406 Not Acceptable in 158ms (ActiveRecord: 21.8ms)

ActionController::UnknownFormat (ActionController::UnknownFormat):

在passwords_controller.rb中:

class Users::PasswordsController < Devise::PasswordsController
  def after_resetting_password_path_for(resource)
    #super(resource)
    root_path
  end
end

在评论 Amit Sharma 后更新:

routes.rb:

Rails.application.routes.draw do

  devise_for :users, controllers: {
        registrations: 'users/registrations',
        passwords: 'users/passwords'
  }
  devise_scope :user do
    post "users/reset_password" => "users/registrations#reset_password", as: 'reset_password'
  end

  resources :cities, :institution_types do
    resources :institutions, only: [:index, :new, :create]
  end
  resources :institutions, only: [:show, :edit, :update, :destroy] do
    resources :proposals, only: [:index, :create]
  end
  get 'institutions/:id/check' => 'institutions#check', as: 'check_path'
  root 'persons#profile'
end

我得到同样的错误:

Started PUT "/users/password" for ::1 at 2016-03-14 17:23:32 +0500
Processing by Users::PasswordsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"QT+j6cLGLyZ1NU3V8aNcXmsmPEAaXxlWizhkaC1z8DqNq85ft6bHNgFKW0r26OzOUjLEPzoYVIQ0khFycRQbxg==", "user"=>{"reset_password_token"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Change my password"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."reset_password_token" =   ORDER BY "users"."id" ASC LIMIT 1  [["reset_password_token", "2a2c916a0611f1944c36f27643edc322e2625284ebf189083774832ac66ee90b"]]
   (0.3ms)  BEGIN
  SQL (0.4ms)  UPDATE "users" SET "encrypted_password" = , "updated_at" = , "reset_password_token" = , "reset_password_sent_at" =  WHERE "users"."id" =   [["encrypted_password", "awrUqD9gr.ZF990WcScPJuzT9ftwxg6xqptkLCKugeQfbrjoQd.5C"], ["updated_at", "2016-03-14 12:23:32.247207"], ["reset_password_token", nil], ["reset_password_sent_at", nil], ["id", 3]]
   (1.0ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.4ms)  UPDATE "users" SET "current_sign_in_at" = , "sign_in_count" = , "updated_at" =  WHERE "users"."id" =   [["current_sign_in_at", "2016-03-14 12:23:32.252785"], ["sign_in_count", 2], ["updated_at", "2016-03-14 12:23:32.253976"], ["id", 3]]
   (0.9ms)  COMMIT
Completed 406 Not Acceptable in 97ms (ActiveRecord: 3.7ms)

ActionController::UnknownFormat (ActionController::UnknownFormat):

原来我没有注意到他的application_controller.rb行respond_to:json。 在 application_controller.rb:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
    respond_to :json
    protect_from_forgery with: :exception

    skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }
    acts_as_token_authentication_handler_for User, fallback: :none

end