2011-08-22 35 views
0

我在3.1.0.rc5应用以下途径Rails的奇异资源简化版,似乎产生“user_path”

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

这里的想法是,我使用设计用于会话管理,“用户“资源只是生成users_path为索引操作,然后其他大多数用户的行动将通过路径来访问像

  • GET user_path - > show动作
  • GET new_user_path - >新动作
  • POST user_path - >创建行动

user_path路线助手似乎并没有被产生,虽然,每当我试图在视图中使用它,我得到一个奇怪的错误时,轨试图渲染它。

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

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

当轨道试图渲染页面,我得到

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"} 
+0

粘贴**耙路线** –

+0

的输出我加耙路线 –

+0

的用户相关的部分我想资源的一点是,没有user_path。你不应该特别需要一个路径给任何用户,因为你只能访问一个,因此名字为'resource'。 – Max

回答

1

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

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