【问题标题】:Rails singular resource does't seem to generate 'user_path'Rails 单一资源似乎不会生成“user_path”
【发布时间】:2011-08-22 00:41:18
【问题描述】:

我的 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'

这是怎么回事!?

编辑这里是 rake 路由的内容。它非常庞大,因此我将其缩减为仅与用户相关的路线。

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

【问题讨论】:

  • 粘贴rake routes的输出
  • 我添加了 rake 路由的用户相关部分
  • 我认为资源的重点是没有user_path。您不需要任何用户的路径,因为您只访问一个用户,因此名称为“资源”。
  • @Max 这就是为什么它是user_path 而不是users_path。它只是不需要 id。
  • 您运行的是什么版本的设计?我只是将一个具有相同 Rails 版本的示例应用程序放在一起,它运行良好。另外,您不能只将单个用户路径命名为“current_user”或“profile”或其他名称并避免所有混乱吗?

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2013-03-20
    • 2018-02-17
    • 2021-10-04
    • 2014-07-15
    • 1970-01-01
    • 2011-05-01
    相关资源
    最近更新 更多