“devise_for”,“devise_scope”的问题!

时间:2011-03-31 16:58:50

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

我正在尝试在设计中自定义我的路线。我曾尝试使用devise_scope:user,但它无法正常工作。所以我改为devise_for,并对自定义路线(注册,确认,密码,会话)进行了改编,并且工作正常。但是,当我在调用“session_path”时,我的视图中会出现错误。它正在将表单重定向到“session.user”,这没有任何意义。

以下是代码:

  #routes.rb

  devise_for :users, :path => '', :skip => [ :passwords, :registrations, :confirmations] do
    post   "account/password",      :to => "devise/passwords#create"
    get    "account/password/new",  :to => "devise/passwords#new", :as => "new_password"
    get    "account/password/edit", :to => "devise/passwords#edit", :as => "edit_password"
    put    "account/password",      :to => "devise/passwords#update"
    post   "account",               :to => "users/registrations#create"
    get    "sign_up",               :to => "users/registrations#new"
    delete "account",               :to => "users/registrations#destroy"
    post   "confirmation",          :to => "devise/confirmations#create"
    get    "confirmation/new",      :to => "devise/confirmations#new", :as => "new_confirmation"
    get    "confirmation",          :to => "devise/confirmations#show"
  end

新会话视图:

  #users/sessions/new.html.erb
  = form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>

错误:

No route matches {:action=>"new", :format=>:user, :controller=>"devise/passwords"}

我该怎么办? “devise_scope”发生的事情无法正常工作(它出现的错误是“找不到...的设计映射”)?

由于

3 个答案:

答案 0 :(得分:6)

正在生成哪些路线?运行rake路线找出来。

如果您没有看到预期的路由,请尝试在devise_for语句后的as块上定义自定义路由。如

devise_for :users, skip => [ :passwords, :registrations, :confirmations]

as :user do
    post   "account/password" => "devise/passwords#create"
    get    "account/password/new" => "devise/passwords#new" 
    get    "account/password/edit" => "devise/passwords#edit" 
    put    "account/password" => "devise/passwords#update"
...

end

您可能还需要自定义视图以指向您声明的新路径。查看Devise wiki以了解如何自定义视图。

答案 1 :(得分:1)

尝试添加:controllers => { :passwords => "devise/passwords"}选项

答案 2 :(得分:1)

我测试了你的路线,我在rake routes

上得到了这个
account_password POST   /account/password(.:format)      {:controller=>"devise/passwords", :action=>"create"}
    new_password GET    /account/password/new(.:format)  {:controller=>"devise/passwords", :action=>"new"}
   edit_password GET    /account/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
                 PUT    /account/password(.:format)      {:controller=>"devise/passwords", :action=>"update"}
         account POST   /account(.:format)               {:action=>"create", :controller=>"users/registrations"}
         sign_up GET    /sign_up(.:format)               {:action=>"new", :controller=>"users/registrations"}
                 DELETE /account(.:format)               {:action=>"destroy", :controller=>"users/registrations"}
    confirmation POST   /confirmation(.:format)          {:action=>"create", :controller=>"devise/confirmations"}
new_confirmation GET    /confirmation/new(.:format)      {:controller=>"devise/confirmations", :action=>"new"}
                 GET    /confirmation(.:format)          {:action=>"show", :controller=>"devise/confirmations"}
new_user_session GET    /sign_in(.:format)               {:action=>"new", :controller=>"devise/sessions"}
    user_session POST   /sign_in(.:format)               {:action=>"create", :controller=>"devise/sessions"}

destroy_user_session DELETE /sign_out(.:format) {:action =>“destroy”,:controller =>“devise / sessions”}

所以,我猜你应该在user_session_path上使用form_for(没有任何参数)。如果你把用户作为一个参数,Rails会认为它是格式,它将无法工作。