[英]Rails model_path routing error, no route matching “model.2”; should be “model/2”
There seems to be something wrong with my routing paths. 我的路由路径似乎有问题。 Normally I should be able to do something like
<%= link_to Profile, user_path(@user||current_user) %>
and I move on with my day. 通常情况下,我应该可以执行类似
<%= link_to Profile, user_path(@user||current_user) %>
然后继续我的一天。 For whatever reason I'm failing to understand my user_path
is not returning /user/:id
like I would expect it to do. 无论出于何种原因,我都无法理解我的
user_path
没有像我期望的那样返回/user/:id
。 Instead it is returning /user.:id
To test this, I loaded a partial with the following code. 相反它返回
/user.:id
为了测试这个,我用以下代码加载了一个部分。
app/view/users/_test.html.rb 应用程序/视图/用户/ _test.html.rb
<%= @user %><br>
<%= @user.id %><br>
<%= link_to user_path(@user), user_path(@user) %><br>
<%= new_user_path %><br>
<%= edit_user_path(@user) %><br>
<%= url_for(@user) %>
This returned 这回来了
localhost:3000/test 本地主机:3000 /测试
#<User:0x007fb6cd341f08>
1
/user.1
/users/new
/users/1/edit
/user.1
I can't figure out what is causing this to happen. 我无法弄清楚导致这种情况发生的原因。 The
edit_user_path(@user)
works perfectly, but the show doesn't. edit_user_path(@user)
工作正常,但节目没有。 I have read the Rails Routing Guide from top to bottom about three times and I can't figure it out. 我已经从上到下阅读了Rails路由指南大约三次,我无法弄明白。 The closest I found to my problem was on an old Rails 3.1 gem problem with Devise , but I'm not even using the Devise gem (maybe I should be?).
我发现问题的最接近的是与Devise的旧Rails 3.1 gem问题 ,但我甚至没有使用Devise gem(也许我应该是?)。
Why is my route failing? 为什么我的路线失败了? I'm not really looking for a workaround (though I suppose I'd rather have a workaround than no solution), I want to understand why this is happening and fix it.
我并不是在寻找一种解决方法(虽然我想我宁愿有一个解决方法而不是解决方案),我想了解为什么会发生这种情况并修复它。 What's going on?
这是怎么回事?
My routes are pretty vanilla, nothing special going on there, but just in case the problem is there and I missed it, here it is. 我的路线非常香草,没什么特别的,但是为了防止出现问题而我错过了,这就是它。
config/routes.rb 配置/ routes.rb中
Rails.application.routes.draw do
root 'static#home'
%w( 404 406 422 500 503 ).each do |code|
get code, :to => "errors#show", :code => code
end
#USER PAGES
get '/test' => 'users#test'
get '/signup' => 'users#new'
post '/user' => 'users#create'
get '/user/list' => 'users#index'
post '/user/' => 'users#update'
get '/user/:id' => 'users#show'
get 'profile', to: 'users#show'
resources :users
end
I'd suggest updating your routes as follows: 我建议更新你的路线如下:
config/routes.rb 配置/ routes.rb中
Rails.application.routes.draw do
root 'static#home'
%w( 404 406 422 500 503 ).each do |code|
get code, :to => "errors#show", :code => code
end
#USER PAGES
get '/test' => 'users#test'
get 'profile', to: 'users#show', as: :user_profile
resources :users
end
...as resources :users
creates all of the routes I've removed. ...作为
resources :users
创建我删除的所有路线。
The duplicates may have been overwriting a default path, causing the behaviour you're seeing - if you pass an object to a url helper that doesn't expect a parameter, you see the behaviour you're getting (ie doesnt_have_an_id_path(@object) => /doesnt_have_an_id.1
). 重复项可能已覆盖默认路径,导致您看到的行为 - 如果您将对象传递给不期望参数的url助手,您会看到您正在获得的行为(即
doesnt_have_an_id_path(@object) => /doesnt_have_an_id.1
)。
I've also added a name to the user profile path to avoid this clashing. 我还在用户配置文件路径中添加了一个名称,以避免这种冲突。 See if this works (perhaps one step at a time to get the cause and effect) - otherwise, try temporarily removing the profile route.
看看是否有效(可能一次只能找到原因和结果) - 否则,请尝试暂时删除配置文件路径。
Re that path, you may have a problem in that the users#show
action will expect an :id
parameter, yet that route doesn't allow for one. 重新启动该路径,您可能会遇到一个问题,即
users#show
action会指望一个:id
参数,但该路由不允许一个。 This might be causing the current problem, though if not, may cause issues down the line. 这可能会导致当前问题,但如果不是,可能会导致问题。
Hope that fixes it - shout if you've any questions / feedback when you've tried it out. 希望能解决它 - 如果您在尝试时有任何问题/反馈,请大声说出来。
In your case the two conflicting routes are: 在您的情况下,两个相互矛盾的路线是:
post '/user/' => 'users#update'
get '/user/:id' => 'users#show'
You are trying to use user_path
helper method, thus you need to have user
named route. 您正在尝试使用
user_path
帮助程序方法,因此您需要具有user
命名的路由。 If you run rake routes
for these two routes you will see this: 如果你为这两条路线运行
rake routes
,你会看到:
Prefix Verb URI Pattern Controller#Action
user POST /user(.:format) users#create
GET /user/:id(.:format) users#show
The Prefix
column shows you the named routes which are defined for your application. “
Prefix
列显示为应用程序定义的命名路由。 In this case Rails auto-generated named route user
for POST /user
endpoint. 在这种情况下,Rails为POST
/user
端点自动生成命名路由user
。 That means user_path
will return /user instead of /user/:id
as you expected. 这意味着
user_path
将按预期返回/ user而不是/user/:id
。 Rails generate routes like these when it sees a route definition without parameters. 当Rails看到没有参数的路由定义时,会生成这样的路由。 For example if you have
get /user/some/more
in your routes, Rails will auto-generate named route user_some_more
for you and they you will be able to use user_some_more_path
helper. 例如,如果你的路由中有
get /user/some/more
,Rails会为你自动生成命名路由user_some_more
,你可以使用user_some_more_path
helper。
In order to fix your particular case you can stop rails from generating route for POST endpoint by doing this: post '/user/', as: nil
and give GET endpoint a name you want: get '/user/:id', as: 'user'
. 为了修复你的特定情况你可以通过这样做来阻止rails为POST端点生成路由:
post '/user/', as: nil
并给GET端点一个你想要的名字: get '/user/:id', as: 'user'
。 Then you will be able to use user_path(user)
to generate paths of format /user/:id. 然后,您将能够使用
user_path(user)
生成格式/ user /:id的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.