In my domain, a user chooses a Universe to edit, then can view/edit/destroy etc. models associated with the Universe they choose. Basically, the entire website depends on which Universe you are currently viewing.
在我的域中,用户选择要编辑的Universe,然后可以查看/编辑/销毁与他们选择的Universe关联的模型等。基本上,整个网站取决于您当前正在查看的Universe。
I had just been passing which Universe in the params, but this is quickly getting untractable. Is there a rails standard way to keep track of what the last Universe a user selected was? Should I store this information in some sort of cookie? Should I make some sort of weird singleton model to keep the data in? Is there way to use the Session (like you would for keeping track of which user is logged in)?
我刚刚通过了参数中的宇宙,但这很快就变得无法解决了。是否有轨道标准方式来跟踪用户选择的最后一个宇宙是什么?我应该将这些信息存储在某种cookie中吗?我应该制作某种奇怪的单例模型来保存数据吗?有没有办法使用Session(就像你要跟踪哪个用户登录一样)?
1 个解决方案
#1
3
How are your routes set up? Use nested routes for any dependencies of that sort:
你的路线是如何建立的?对这种类型的任何依赖项使用嵌套路由:
resources :universes do
resources :planets
resources :spaceships
end
The urls to your pages then look like this:
您网页的网址如下所示:
www.somedomain.com/:universe_id/planets/:planet_id/edit?param=1¶m2=2
Then you always have the universe id available and you can use a before_filter in your ApplicationController to call a function which loads the universe before any action, so you always have the @universe available without you having any work.
然后,您始终可以使用Universe ID,并且可以在ApplicationController中使用before_filter来调用在执行任何操作之前加载Universe的函数,这样您就可以在没有任何工作的情况下使用@universe。
Of course you still have to add the universe to your links but that can also be automated by creating helper methods which makes use of your always present @universe object. The default helpers created for these resource routes look like edit_universe_planet_path(@universe, planet.id)
当然,您仍然需要将Universe添加到链接中,但也可以通过创建辅助方法来自动化,这些方法使用您始终存在的@universe对象。为这些资源路由创建的默认帮助程序类似于edit_universe_planet_path(@universe,planet.id)
#1
3
How are your routes set up? Use nested routes for any dependencies of that sort:
你的路线是如何建立的?对这种类型的任何依赖项使用嵌套路由:
resources :universes do
resources :planets
resources :spaceships
end
The urls to your pages then look like this:
您网页的网址如下所示:
www.somedomain.com/:universe_id/planets/:planet_id/edit?param=1¶m2=2
Then you always have the universe id available and you can use a before_filter in your ApplicationController to call a function which loads the universe before any action, so you always have the @universe available without you having any work.
然后,您始终可以使用Universe ID,并且可以在ApplicationController中使用before_filter来调用在执行任何操作之前加载Universe的函数,这样您就可以在没有任何工作的情况下使用@universe。
Of course you still have to add the universe to your links but that can also be automated by creating helper methods which makes use of your always present @universe object. The default helpers created for these resource routes look like edit_universe_planet_path(@universe, planet.id)
当然,您仍然需要将Universe添加到链接中,但也可以通过创建辅助方法来自动化,这些方法使用您始终存在的@universe对象。为这些资源路由创建的默认帮助程序类似于edit_universe_planet_path(@universe,planet.id)