Rails奇异资源似乎不会生成'user_path'

时间:2011-08-22 00:41:18

标签: ruby-on-rails ruby-on-rails-3 routes ruby-on-rails-3.1

我的3.1.0.rc5应用程序中有以下路由

# config/routes.rb
devise_for :users
resources :users, only: :index
resource :user

这里的想法是我使用设备进行会话管理,'用户'资源只是为索引操作生成users_path,然后大多数其他用户操作可以通过

等路径访问
  • GET user_path - >展示行动
  • GET new_user_path - >新行动
  • POST user_path - >创造行动

虽然似乎没有生成user_path路由助手,但每当我尝试在视图中使用它时,当rails尝试渲染它时,我会收到一个奇怪的错误。

例如,在/user/new页面上,我有以下内容

<%= form_for @user, :url => user_path do |f| %>
  # omitted form elements
<% end %>

当rails尝试渲染页面时,我得到了

ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"users"}):
    7: </div>
    8: 
    9: <div class="content_middle">
    10:   <%= form_for @user, :url => user_path do |f| %>
    11:   <fieldset>
    12:     <%= render partial: "form_errors" %>
    13: 
  app/views/users/new.html.erb:10:in `_app_views_users_new_html_erb___1548382046039026466_2191201580'

这是怎么回事??

编辑以下是佣金路线的内容。它非常庞大,所以我把它简化为与用户相关的路线。

       new_user_session GET    /users/sign_in(.:format)                     {:action=>"new", :controller=>"devise/sessions"}
           user_session POST   /users/sign_in(.:format)                     {:action=>"create", :controller=>"devise/sessions"}
   destroy_user_session DELETE /users/sign_out(.:format)                    {:action=>"destroy", :controller=>"devise/sessions"}
          user_password POST   /users/password(.:format)                    {:action=>"create", :controller=>"devise/passwords"}
      new_user_password GET    /users/password/new(.:format)                {:action=>"new", :controller=>"devise/passwords"}
     edit_user_password GET    /users/password/edit(.:format)               {:action=>"edit", :controller=>"devise/passwords"}
                        PUT    /users/password(.:format)                    {:action=>"update", :controller=>"devise/passwords"}
                  users GET    /users(.:format)                             {:action=>"index", :controller=>"users"}
                        POST   /users(.:format)                             {:action=>"create", :controller=>"users"}
                   user DELETE /users/:id(.:format)                         {:action=>"destroy", :controller=>"users"}
                        POST   /user(.:format)                              {:action=>"create", :controller=>"users"}
               new_user GET    /user/new(.:format)                          {:action=>"new", :controller=>"users"}
              edit_user GET    /user/edit(.:format)                         {:action=>"edit", :controller=>"users"}
                        GET    /user(.:format)                              {:action=>"show", :controller=>"users"}
                        PUT    /user(.:format)                              {:action=>"update", :controller=>"users"}

1 个答案:

答案 0 :(得分:1)

在这种情况下拥有ProfileCurrentUser控制器可能是有意义的。它可以消除您在Devise中可能遇到的任何路由冲突,而且,从RESTful的角度来看,当您将当前用户视为独特的资源时,这是有道理的。

devise_for :user
resources :users, :only => :index
resource :profile