显示自定义设计在任何地方忘记密码表单

时间:2014-03-02 19:27:05

标签: ruby-on-rails ruby authentication ruby-on-rails-4 devise

我想在任何地方显示自定义Devise'忘记密码'表单,我认为它类似于https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

所以在config/routes.rb我添加了:

get "/forgot_password" => "desktop#new"

app/views/desktop/new.html.haml

      =form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f|
        .error-messages
          =devise_error_messages!
        =f.email_field :email, :autofocus => true, :required => true, :class => 'field'
        %br
        %br
        %button.signin-button Send Instructions

app/controllers/desktop_controller.rb

class DesktopController < ApplicationController
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end

  def new
  end
end

当我导航到/ forgot_password时,出现undefined local variable or method 'resource'错误。我做错了什么?

2 个答案:

答案 0 :(得分:1)

原来我需要

helper_method :resource, :resource_name, :devise_mapping

在desktop_controller.rb中的方法之后

答案 1 :(得分:0)

最好在应用程序助手中定义这些方法

module ApplicationHelper

def resource_name
   :user
  end

  def resource
   @resource ||= User.new
  end

 def devise_mapping
 @devise_mapping ||= Devise.mappings[:user]
 end
 end