说我的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
能够访问它,还是有更好的方法来实现它?
答案 0 :(得分:1)
要回答问题的第一部分,在application_helper中你可以创建一个这样的函数:
def planet_path(planet)
universe_planet_path(planet.universe, planet)
end
虽然不确定form_for
部分,但这有点棘手。