1

我收到一个错误:

NoMethodError in Devise/registrations#new

undefined method 'user_registration_path'

在这一行:

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

这是我的路线:

  devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do 
    # devise/registrations

    get 'signup' => 'devise/registrations#new', :as => :new_user_registration 
    post 'signup' => 'devise/registrations#create', :as => :custom_user_registration 
  end

耙路线:

      new_user_registration GET    /signup(.:format)      devise/registrations#new
   custom_user_registration POST   /signup(.:format)      devise/registrations#create

为什么我会收到user_registration_path错误消息?

4

2 回答 2

4

运行 rake 路由,您是否在输出中看到如下内容:

user_registration POST    /users(.:format)     devise/registrations#create

我想如果你写了这行

post 'signup' => 'devise/registrations#create', :as => :custom_user_registration

现在你有:

custom_user_registration POST    /signup(.:format)     devise/registrations#create

并且应该使用custom_user_registration_path(resource_name)而不是registration_path(resource_name)

于 2013-08-22T03:24:24.187 回答
1

您正在跳过注册路由,而不是用您定义的自定义路由覆盖它们。:registrations从跳过的路线中删除。在您的 routes.rb 中:

这个

devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do

应该

devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:sessions] do

或者,如果您希望路径始终为 /signup,则可以添加这些自定义路由:

devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do 
  get 'signup' => 'devise/registrations#new', :as => :new_user_registration 
  post 'signup' => 'devise/registrations#create', as => :user_registration
  delete 'signup' => 'devise/registrations#destroy', as => :destroy_user_registration
end

我不建议更改助手名称 ( as => :whatever),因为设计控制器和视图使用它们。添加新的很好。如果它是默认的,我不确定你是否需要指定控制器。

于 2013-08-22T03:32:01.230 回答