手动路由和“找不到 id=new 的用户";错误 [英] Manual routes and "Couldn't find User with id=new" error

查看:23
本文介绍了手动路由和“找不到 id=new 的用户";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的用户模型手动创建路由(而不是使用resource: users"),

I'm trying to create routes manually for my users model ( not with "resource: users"),

所以在 routes.rb 中:

get '/users/:id', to: 'users#show', as: 'user'
get '/users/new', to: 'users#new', as: 'new_user'

但是当我尝试访问 /users/new我得到 找不到 id=new 的用户".

But when I'm trying to go to /users/new i get "Couldn't find User with id=new".

我确实理解为什么会发生这种情况,但我想找到允许它原样的方法(不更改其中一个路径).怎么可能?

I do understand why it happens, but I want to find way to allow it as is (without changing one of the paths). how does it possible?

推荐答案

路由的顺序很重要.因为您已经在 new 路由之前定义了 show 路由,所以 show 操作将使用 id 作为.

The order of routes is important. Because you've defined your show route before the new route, the show action is getting executed with id as new.

交换两条路线的位置:

get '/users/new', to: 'users#new', as: 'new_user'
get '/users/:id', to: 'users#show', as: 'user'

这篇关于手动路由和“找不到 id=new 的用户";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆