【问题标题】:Rails error (Couldn't find User with 'id'=logout) with Postgres使用 Postgres 的 Rails 错误(找不到具有 'id'=logout 的用户)
【发布时间】:2015-03-14 21:52:11
【问题描述】:

(找不到具有 'id'=logout 的用户)的可能解决方案是什么?。我很感激任何意见。我能找到的只是对错误的引用。提前致谢。

ActiveRecord::RecordNotFound (Couldn't find User with 'id'=logout):
  app/controllers/users_controller.rb:16:in `show'

users_controller.rb

line 15: def show
line 16:   @user = User.find(params[:id])
line 17: end

routes.rb

delete 'logout' => 'sessions#destroy'

views/layout/application.html.erb

          <% if logged_in? %>
            <li><%= link_to current_user.name, current_user %></li>
            <li><%= link_to "Log out", logout_path, method: :delete %></li>
          <% else %>
            <li><%= link_to "Log in", login_path %></li>
          <% end %>

【问题讨论】:

  • 你能发布更多或你的代码。喜欢你的链接到logout in view
  • 我添加了相关的注销视图。感谢您的帮助。
  • 能否发布您的整个 routes.rb 和 rake routes 的输出?
  • 尝试用这个delete ':id', to: 'sessions#destroy', as: :logout替换路由文件中的那一行

标签: ruby-on-rails postgresql webrick


【解决方案1】:

所以你点击了logout_path 生成的 URL(所以,/logout?),而不是去你的sessions#destroy 操作,而是去你的users#show 操作并且失败,因为你的操作被赋予了字符串"logout" in params[:id]

这将由您的 routes.rb 文件中较早的另一条路线引起,该路线遮蔽了您在问题中显示的路线。例如。你可能有这样的事情:

resources :users, path: '/'

...或者你有一个普通的用户资源,但是你的 delete 行嵌套在用户命名空间或其他东西中:

resources :users

# then lower in your file something like...
namespace :users do
  delete 'logout' => 'sessions#destroy'
end

更新

这些事情都不会导致您所看到的,所以如果上述内容没有为您指明正确的方向,那么请在您的问题中添加完整的config/routes.rb 文件和/或rake routes 的输出

【讨论】:

    【解决方案2】:

    由于某种原因,Firefox 在单击注销时没有发送 http 删除请求,而是发送了一个 get,因此必须在 routes.rb 中添加一行来处理注销的 http get 请求。特别感谢约翰!

    【讨论】:

    • 谢谢大家!如果您有其他解决方案,请分享。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    相关资源
    最近更新 更多