Rails路由问题:直接在根目录下映射子段/永久链接? [英] Rails Routing Question: Mapping Slugs/Permalinks Directly under Root?

查看:76
本文介绍了Rails路由问题:直接在根目录下映射子段/永久链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好!..

一般路由问题在这里...我目前正在为我的应用程序中的用户实现类似于此的路由.

General Routing Quesiton Here... I'm currently working to achieve a route similar to this for users in my application.

http://www.example.com/ 用户名

然后将其映射到usersControllers#show,因此我的路由文件中包含以下内容.

This then maps to the usersControllers#show, hence I have the following in my routes file.

map.connect'/:permalink',:controllers =>"users",:action =>"show"

map.connect '/:permalink', :controllers => "users", :action => "show"

然后,我执行了show操作,以通过参数中的固定链接找到用户.所以它的作品,但是...

I've then got the show action to find the user by the permalink in the param. So its works but....

我遇到的问题是所有其他未定义的路由都发送到userController#show.即404和其他未命名的路线.所以我不认为我为此选择了正确的约定.我的解决方案是在此之上添加其他命名的路由,这可以解决问题,但对我来说似乎很脆弱.我在想这个错误吗?

The problem I'm running into is that all other UNDEFINED routes get sent to userController#show. i.e 404's & other un-named routes. So I dont think i'm going with the right convention for this. My solution is to just add other named routes above this, which solves the problem, but to me seems brittle. Am I thinking about this wrong?

什么是更好的解决方案?我将向Google寻求答案,但我只是想将其用于讨论.想法?

Whats a better solution? I'm going to mine google for answers but I just thought i'd throw this up for discussion. Ideas?

推荐答案

您做对了. Rails路由从顶部的高优先级到底部的低优先级.您的用户显示操作应该在底部.只要确保如果永久链接与用户不对应,就会生成正确的404.

You're doing it right. Rails routes go from high priority at the top to low priority at the bottom. Your users show action should go at the bottom. Just make sure that if the permalink does not correspond to a user a proper 404 is generated.

这篇关于Rails路由问题:直接在根目录下映射子段/永久链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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