0
我有一個簡單的管理頁面用於通過單擊一個鏈接來批准新用戶。如何通過註冊控制器阻止我的定製設計用戶更新操作
的意見/用戶/ index.html.erb<%= link_to "Approve User", user_path(:id => user.id, "user[approved]" => true), :remote => true, :method => :patch %>
在users_controller.rb在routes.rb中
devise_for :users, :controllers => {:registrations => 'users/registrations', :sessions => 'users/sessions'}
resources :users, only: [:index, :show, :update]
此鏈接
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to users_path
else
logger.debug "ADMIN UPDATE OF USER DID NOT WORK"
end
end
,即用戶
已更新爲已批准。但行動通過用戶控制器後,它也通過註冊控制器..
Started PATCH "https://stackoverflow.com/users/6?user%5Bapproved%5D=true" for ::1 at 2015-05-02 21:27:34 -0700
Processing by UsersController#update as JS
Parameters: {"user"=>{"approved"=>"true"}, "id"=>"6"}
User Load (1.4ms) SELECT "t_customer".* FROM "t_customer" WHERE "t_customer"."cust_id" = $1 ORDER BY "t_customer"."cust_id" ASC LIMIT 1 [["cust_id", 2]]
User Load (0.8ms) SELECT "t_customer".* FROM "t_customer" WHERE "t_customer"."cust_id" = $1 LIMIT 1 [["cust_id", 6]]
(0.2ms) BEGIN
SQL (0.6ms) UPDATE "t_customer" SET "approved" = $1, "updated_at" = $2 WHERE "t_customer"."cust_id" = $3 [["approved", "t"], ["updated_at", "2015-05-03 04:27:34.933469"], ["cust_id", 6]]
(2.0ms) COMMIT
Redirected to http://localhost:3000/users
Completed 302 Found in 21ms (ActiveRecord: 5.0ms)
Started PATCH "/users" for ::1 at 2015-05-02 21:27:34 -0700
Processing by Users::RegistrationsController#update as JS
User Load (0.5ms) SELECT "t_customer".* FROM "t_customer" WHERE "t_customer"."cust_id" = $1 ORDER BY "t_customer"."cust_id" ASC LIMIT 1 [["cust_id", 2]]
...
如何阻止它通過註冊控制器?我覺得我不得不以某種方式限制它在routes.rb中,但我不知道如何。
我發現此錯誤的原因是因爲當用戶單擊「批准用戶」鏈接時,用戶索引頁面沒有正確刷新,表明用戶現在已被批准。如果我通過瀏覽器刷新它,則會顯示更改。我需要更改才能在用戶控制器中顯示redirect_to調用。一旦註冊控制器被切斷流程,會發生這種情況嗎?
這個工作適合你嗎?當我使用你的解決方案時,我得到了'ActiveModel :: ForbiddenAttributesError',它在日誌中顯示回滾。我認爲Devise會覆蓋用戶控制器中更新方法的能力。你沒有這個問題? – dgreen22