没有路由匹配{:action=>"destroy", :controller=>"users"}。

10

当我尝试通过命名路由 (http://localhost:3000/profile/) 访问 users#show 页面时,我遇到了错误... 但是当我使用标准的 URL (http://localhost:3000/users/current) 访问时没有出现错误。如果我使用 rake routes 命令,则路由似乎正确,因为它可以正常工作,所以我真的不知道为什么会出现 No route matches 错误。为什么在我甚至没有尝试访问它时要查找动作 'destroy' 的路由匹配?

Starcast::Application.routes.draw do

  match "login" => 'user_sessions#new', :as => :login
  match "logout" => 'user_sessions#destroy', :as => :logout
  resources :user_sessions 

  match "profile" => 'users#show'
  resources :users

  resources :casters
  resources :casts
  resources :orders

  root :to => "home#index"

end

我得到的错误:

ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"users"}):
    1: <% title "Welcome #{@user.username}" %>
    2: 
    3: <%= link_to "Edit your profil", edit_user_path %> 
    4: 
    5: <% has_role? :caster do %>
    6: <% if @user.caster %>
  app/views/users/show.html.erb:3:in `_app_views_users_show_html_erb___2116234531537545622_2170017780__3613739707062673465'

1
编辑/显示/删除路径需要一个id参数...即edit_user_path(current_user.id)...如果你不想这样做,你需要让你的路由使用"resource :user"(而不是"resources :user"),如果你不正确地这样做,以后会带来很多麻烦。 - jenjenut233
谢谢。您可以将其以回答的形式写出来,这样我就确认一下。非常感谢! - plehoux
1个回答

25

编辑/显示/删除/更新路径需要一个id参数...比如edit_user_path(current_user.id)... 如果你不想用这种方式,那么你需要使用resource:user(而不是resources:user)来设置路由,否则如果你做得不对,以后会带来很多麻烦。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接