Rails 2.3.5:如何获取路线名称 [英] Rails 2.3.5: how to get routes name

查看:82
本文介绍了Rails 2.3.5:如何获取路线名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序route.rb中,我定义了以下三种路由

  map.signup'/ signup',:控制器=> ‘用户’,:action => 'new'
map.login'/ login',:controller => 会话,:action => ‘new’
map.logout‘/ logout’,:controller => 会话,:action => 'destroy'

我是否可以获取特定路径的控制器和动作名称? / p>

我正在寻找这样的方法...

  def current_routes(a)
结束

应返回:controller => ; ‘用户’,:action => 'new'如果我调用 current_routes('signup_path')

解决方案

像这样

 
ActionController :: Routing :: Routes.recognize_path( / posts /)

如果您的路由中只有一个字符串(例如 signup_path),那么我想在上下文中您应该使用能够做到

 
ActionController :: Routing :: Routes.recognize_path(send( signup_path .to_sym))


In my applications routes.rb I have defined three routes like the following

 map.signup '/signup', :controller => 'users', :action => 'new'
 map.login  '/login', :controller => 'sessions', :action => 'new'
 map.logout '/logout', :controller => 'sessions', :action => 'destroy'

Is it possible for me to get the controller and action name for a particular path?

I am looking for some method like this...

def current_routes(a)
end

should return :controller => 'users', :action => 'new' if I call current_routes('signup_path')

解决方案

Try like this

ActionController::Routing::Routes.recognize_path("/posts/")

If you only have a string with your route (like "signup_path"), then I guess in the context you're using this you should be able to do

ActionController::Routing::Routes.recognize_path(send("signup_path".to_sym))

这篇关于Rails 2.3.5:如何获取路线名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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