【问题标题】:rails devise edit_user_registrationrails 设计 edit_user_registration
【发布时间】:2013-02-06 14:18:07
【问题描述】:

我在我的 rails 应用程序中使用 devise 进行身份验证。我可以登录和注销,也可以注册。但是,一旦用户登录,我想为用户提供一种修改登录密码的方法。为此,该应用程序提供了一个指向 'edit_user_registration_path' 的链接,该链接显示在下面的 rake 路由输出中。

当用户点击下方修改密码链接时,

<li><%= link_to "Change Password", edit_user_registration_path %></li>

我收到以下错误

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"users"}):

我有一个带有显示、编辑和更新操作的 UsersController。编辑操作是更新用户模型的其他列。

路线设置如下

devise_for :users
resources  :users, only: [:show, :edit, :update]

rake 路由输出 -

    new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
        user_session POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
       user_password POST   /users/password(.:format)      devise/passwords#create
   new_user_password GET    /users/password/new(.:format)  devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                     PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
   user_registration POST   /users(.:format)               devise/registrations#create
new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                     PUT    /users(.:format)               devise/registrations#update
                     DELETE /users(.:format)               devise/registrations#destroy
           edit_user GET    /users/:id/edit(.:format)      users#edit
                user GET    /users/:id(.:format)           users#show
                     PUT    /users/:id(.:format)           users#update
                root        /                              home#index

控制台日志如下

Started GET "/users/edit" for 127.0.0.1 at 2013-02-21 15:21:38 +0530
Processing by Devise::RegistrationsController#edit as HTML
 User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
 Rendered devise/registrations/edit.html.erb within layouts/application (15.9ms)
 Rendered layouts/_shim.html.erb (0.0ms)
 Rendered layouts/_messages.html.erb (0.1ms)
 Rendered layouts/_header.html.erb (42.5ms)
Completed 500 Internal Server Error in 99ms

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"users"}):
app/views/layouts/_header.html.erb:22:in `_app_views_layouts__header_html_erb__267315279__621159918'
app/views/layouts/application.html.erb:20:in `_app_views_layouts_application_html_erb__982964301_89714280'

【问题讨论】:

  • 你能给我们看看:app/views/layouts/_header.html.erb:22
  • 我相信你是在自己调用edit_user_path,而没有告诉它要编辑哪个用户。
  • 如你所料,我使用的是edit_user_path。这有什么问题吗?如何在此处指示用户?
  • @Leito,正如您所建议的,我通过添加要编辑的用户来修改 edit_user_path,一切正常。编辑用户路径(当前用户)。谢谢!
  • 标签: ruby-on-rails devise


    【解决方案1】:

    以上日志中重要的几行是:

    ActionController::RoutingError (没有路由匹配 {:action=>"edit", :controller=>"users"}): app/views/layouts/_header.html.erb:22:in `_app_views_layouts__header_html_erb__267315279__621159918'

    路由需要匹配它的所有参数,在这种情况下

    edit_user GET    /users/:id/edit(.:format)      users#edit
    

    在没有提供 :id 时不匹配。

    代替:

    <%= link_to "Settings", edit_user_path %>
    

    发送current_user作为参数:

    <%= link_to "Settings", edit_user_path(current_user) %>
    

    【讨论】:

    • 我接受您的回答,因为您的上述评论解决了问题。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签