Devise的registration_path,它是如何生成的?

时间:2013-10-28 21:48:12

标签: ruby-on-rails devise

默认设计用户注册表单如下所示:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

当我运行rake routes时,我看不到任何registration前缀,有user_registrationnew_user_registration等,但不只是registration,所以如何它有用吗?我在哪里可以找到它的源代码?

2 个答案:

答案 0 :(得分:2)

生成registration_path路由 Devise::Controllers::UrlHelpers

正如您在下面的代码中所看到的,它只是调用您的常规路线。

def self.generate_helpers!(routes=nil)
  routes ||= begin
    mappings = Devise.mappings.values.map(&:used_helpers).flatten.uniq
    Devise::URL_HELPERS.slice(*mappings)
  end

  routes.each do |module_name, actions|
    [:path, :url].each do |path_or_url|
      actions.each do |action|
        action = action ? "#{action}_" : ""
        method = "#{action}#{module_name}_#{path_or_url}"

        class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
          def #{method}(resource_or_scope, *args)
            scope = Devise::Mapping.find_scope!(resource_or_scope)
            _devise_route_context.send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
          end
        URL_HELPERS
      end
    end
  end
end

generate_helpers!(Devise::URL_HELPERS)

Devise::URL_HELPERS为:

{:session=>[nil, :new, :destroy], :omniauth_callback=>[], :password=>[nil, :new, :edit], :registration=>[nil, :new, :edit, :cancel], :confirmation=>[nil, :new], :unlock=>[nil, :new]}

答案 1 :(得分:1)

将以下内容放入routes.rb

 devise_for :users, :controllers => {:sessions => 'devise/sessions', :registrations => 'devise/registrations',
                                      :passwords => 'devise/passwords'}, :skip => [:sessions] do
    get '/login' => 'devise/sessions#new', :as => :new_user_session
    post '/login' => 'devise/sessions#create', :as => :user_session
    get '/logout' => 'devise/sessions#destroy', :as => :destroy_user_session
  end

然后运行rake路线。您可以在List of Devise Controllers

找到设计控制器