5

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

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

当我运行时,rake routes我看不到任何registration前缀,有user_registrationnew_user_registration等等,但不仅仅是registration,那么它是如何工作的?我在哪里可以找到它的源代码?

4

2 回答 2

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]}
于 2014-03-13T10:12:46.910 回答
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 路线。您可以在以下位置找到设计控制器:设计控制器列表

于 2013-10-28T21:51:56.167 回答