将模型的路径重定向到Rails中的不同路径

时间:2013-04-06 00:09:51

标签: ruby-on-rails

说我的routes.rb中有以下内容:

resources :universes do
    resources :planets
end

这会创建一个方法universe_planet_path(universe, planet)。但是,行星对象知道它属于哪个宇宙,因此我希望有planet_path(planet)函数可以返回与universe_planet_path(planet.universe, planet)相同的URL。我应该在哪里放置这样一个函数,比如form_for @planet能够访问它,还是有更好的方法来实现它?

1 个答案:

答案 0 :(得分:1)

要回答问题的第一部分,在application_helper中你可以创建一个这样的函数:

def planet_path(planet)
    universe_planet_path(planet.universe, planet)
end

虽然不确定form_for部分,但这有点棘手。