找到你要的答案

Q:how to stop my custom devise user update action from going through the registrations controller

Q:如何停止我的自定义设计用户更新行动,通过登记控制器

I have a simple admin page for approving new users by clicking a single link.

in views/users/index.html.erb

<%= link_to "Approve User", user_path(:id => user.id, "user[approved]" => true), :remote => true, :method => :patch %>

in users_controller.rb

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

in routes.rb

devise_for :users, :controllers => {:registrations => 'users/registrations', :sessions => 'users/sessions'}

resources :users, only: [:index, :show, :update]

This link works, i.e. the user is updated to approved. But after the action goes through the users controller, it also goes through the registrations controller..

Started PATCH "/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]]
...

How do I stop it from going through the registrations controller? I feel like I have to restrict it somehow in routes.rb, but I don't know how.

The reason I caught this error is because when the user clicks the "approve user" link, the users index page does not properly refresh indicating that the user is now approved. If I refresh it via the browser, then the change is indicated. I need the change to show up with the redirect_to call in the users controller. Will this happen once the registrations controller is cut out of the process?

我有一个简单的管理页面批准新用户单击一个链接。

在视图/用户/ index.html.erb

<%= link_to "Approve User", user_path(:id => user.id, "user[approved]" => true), :remote => true, :method => :patch %>

在users_controller.rb

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

在routes.rb

devise_for :users, :controllers => {:registrations => 'users/registrations', :sessions => 'users/sessions'}

resources :users, only: [:index, :show, :update]

此链接工作,即用户更新到批准。但经过用户控制器的动作,它也通过注册控制器..

Started PATCH "/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]]
...

How do I stop it from going through the registrations controller? I feel like I have to restrict it somehow 在routes.rb, but I don't know how.

之所以我发现这个错误是因为当用户单击“批准用户”链接时,用户索引页没有正确刷新,表明用户已被批准。如果我刷新它通过浏览器,然后更改表示。我需要改变,显示在用户的redirect_to电话控制器。一旦注册控制器退出过程,这种情况会发生吗?

answer1: 回答1:

I removed :remote => true from my link_to's and it's no longer going through the registrations controller and the update is appearing on page refresh.

我删除:远程= & gt;真的从我的link_to和它不再通过注册控制器和更新出现在页面刷新。

ruby-on-rails  devise  controller