ruby on rails 中的自定义 url [英] Custom url in ruby on rails

查看:50
本文介绍了ruby on rails 中的自定义 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我知道 rails 使用控制器动作样式的 url,例如 www.myapp.com/home/index
我想在我的 rails 应用程序上有一个这样的 url,www.myapp.com/my_page_here 这可能吗,如果可能,我该怎么做?


I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?

推荐答案

您只需在任何 resourcesnamespace 块之外使用 get在您的 routes.rb 文件中:

You just use a get outside of any resources or namespace block in your routes.rb file:

get 'my_page_here ', :to => 'home#index'

假设您使用的是 Rails 3+,请不要使用 match.这可能很危险,因为如果页面接受来自表单的数据,它应该接受 POST 请求.match 将允许对具有副作用的操作进行 GET 请求 - 这并不好.

Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.

尽可能使用 getputpost 或这些变体.

Always use get, put, post or these variants where possible.

要获取路径助手,请尝试:

To get a path helper, try:

get 'my_page_here ', :to => 'home#index', :as => :my_page

这样,在您的视图中,my_page_path 将等于 http://{domain}/my_page_here

That way, in your views, my_page_path will equal http://{domain}/my_page_here

这篇关于ruby on rails 中的自定义 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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