【问题标题】:Overriding Devise Passwords Controller覆盖设计密码控制器
【发布时间】:2012-11-16 08:27:52
【问题描述】:

我想禁用

  def create
    self.resource = resource_class.send_reset_password_instructions(resource_params)

    if successfully_sent?(resource)
      respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
    else
      respond_with(resource)
    end
  end

所以在发送重置密码后它根本不会重定向

所以,我在 app/controllers/users/ 下创建了一个名为 passwords_controller.rb 的新文件

看起来像这样

class User::PasswordsController < Devise::PasswordsController

  def create
    self.resource = resource_class.send_reset_password_instructions(resource_params)

    if successfully_sent?(resource)
      flash[:notice] = "sent password"
    else
      respond_with(resource)
    end
  end

  def new
    super
  end

  def update
    super       
  end

  def edit
    super   
  end
end

并改变了我的路线

 devise_for :users, :controllers => { :invitations => 'users/invitations', :passwords => 'users/passwords' }

我也有 devise_invite gem..

当我点击忘记密码的链接时出现此错误

Started GET "/users/password/new" for 127.0.0.1 at 2012-11-16 10:21:07 +0200

ActionController::RoutingError (uninitialized constant Users::PasswordsController):

我的rake routes

              user_password POST   /users/password(.:format)                  users/passwords#create
          new_user_password GET    /users/password/new(.:format)              users/passwords#new
         edit_user_password GET    /users/password/edit(.:format)             users/passwords#edit
                            PUT    /users/password(.:format)                  users/passwords#update

视图中的链接是

<%= link_to "Forgot your password?", new_password_path(User) , :class => "control-group", :style => "position: absolute; bottom: 0", :id=>"forgotpass" %>

我错过了什么?

【问题讨论】:

  • 可能是因为错别字
  • 我不明白在哪里
  • 在你看来,把new_password_path(User)改成new_user_password_path
  • 可能需要把User::PasswordsController改成Users::PasswordsController

标签: ruby-on-rails-3 inheritance devise controller


【解决方案1】:

密码控制器是从设计密码控制器扩展而来的。因此,使用设计密码控制器扩展密码控制器。

class PasswordsController < Devise::PasswordsController
  ......................

end

使用 devise 更改密码控制器的路由

devise_for :users, :controllers => { :passwords => "passwords" }

路线会是这样的:-

user_password       POST   /password(.:format)       Passwords#create

new_user_password   GET    /password/new(.:format)   Passwords#new

edit_user_password  GET    /password/edit(.:format)  Passwords#edit
                    PUT    /password(.:format)       Passwords#update

【讨论】:

  • 不需要devise_for :users, :controllers =&gt; { :passwords =&gt; "passwords" }加上小写的“p”吗?
  • 是的,{ :passwords =&gt; "Passwords" } 没用。使用{ :passwords =&gt; "passwords" }
【解决方案2】:

如果您想保留命名空间,请尝试:

# routes.rb
devise_for :users, controllers: { passwords: 'users/passwords' }

# users/passwords_controller.rb
class Users::PasswordsController < Devise::PasswordsController
  ...
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    相关资源
    最近更新 更多