如何在设计gem中的更新控制器(编辑用户)中添加调用操作?

时间:2012-07-19 11:41:25

标签: ruby-on-rails devise

我需要在用户编辑页面时调用函数。

这是我的settings_controller.rb:

     class SettingsController < ApplicationController
def update
  @user = User.find(current_user.id)
  email_changed = @user.email != params[:user][:email]
  password_changed = !params[:user][:password].empty?
  successfully_updated = if email_changed or password_changed
    @user.update_with_password(params[:user])
  else
    @user.update_without_password(params[:user])
  end

  if successfully_updated
    # Sign in the user bypassing validation in case his password changed
    sign_in @user, :bypass => true
    //need to call action here
  else
    render "edit"
  end
end

如果成功更新,我需要重定向用户。

在我的routes.rb中:

   devise_for :users, :controllers => {:registrations => 'registrations', :settings => 'settings'}

或者我做错了什么?

1 个答案:

答案 0 :(得分:2)

settings中没有devise_for路由。

查看RegistrationController in devise.

您可以覆盖此内容:

class SettingsController < Devise::RegistrationsController
 def update
  @user = User.find(current_user.id)
  email_changed = @user.email != params[:user][:email]
  password_changed = !params[:user][:password].empty?
  successfully_updated = if email_changed or password_changed
    @user.update_with_password(params[:user])
  else
    @user.update_without_password(params[:user])
  end

  if successfully_updated
    # Sign in the user bypassing validation in case his password changed
    sign_in @user, :bypass => true
    //need to call action here
  else
    render "edit"
  end
 end
end

路由:

devise_for :users, :controllers => {:registrations => 'settings'}