我使用devise和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一切正常,它带有一个链接的电子邮件消息。当您单击链接时,将打开默认视图设计/密码/编辑。我输入一个新密码,我点击提交,然后收到错误:
用户中的ActionController :: UnknownFormat :: 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" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["reset_password_token", "0733ad150b4a7d695e5125a79271f96fd7fa140d195480dcb215ecc03c048c6c"]]
(0.2ms) BEGIN
SQL (0.6ms) UPDATE "users" SET "encrypted_password" = $1, "updated_at" = $2, "reset_password_token" = $3, "reset_password_sent_at" = $4 WHERE "users"."id" = $5 [["encrypted_password", "$2a$10$2uUu/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" = $1, "current_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = $5 [["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):
密码_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" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["reset_password_token", "2a2c916a0611f1944c36f27643edc322e2625284ebf189083774832ac66ee90b"]]
(0.3ms) BEGIN
SQL (0.4ms) UPDATE "users" SET "encrypted_password" = $1, "updated_at" = $2, "reset_password_token" = $3, "reset_password_sent_at" = $4 WHERE "users"."id" = $5 [["encrypted_password", "$2a$10$5wrUqD9gr.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" = $1, "sign_in_count" = $2, "updated_at" = $3 WHERE "users"."id" = $4 [["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):
答案 0 :(得分:2)
原来我在他的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