Rails:从URL解析路线信息 [英] Rails: Parse route information from URL

查看:91
本文介绍了Rails:从URL解析路线信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将网址字符串解析为哈希,例如

How can I parse url string to hash like

{:controller => 'controller_name', :action => 'action_name', :id => 'id'}

推荐答案

在Rails 3中,您可以执行以下操作:

In Rails 3 you can do the following:

Rails.application.routes.recognize_path "/accounts/1"
# {:action=>"show", :controller=>"accounts", :id=>"1"}

使用ActionController :: Routing :: Routes.recognize_path不断抛出ActionController :: RoutingError异常:没有与 / accounts / 1匹配的路由

Using ActionController::Routing::Routes.recognize_path kept throwing ActionController::RoutingError Exception: No route matches "/accounts/1

这篇关于Rails:从URL解析路线信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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