Pav*_* S. 7 parameters persistence controller ruby-on-rails
有没有办法在Rails控制器中保留(保留)参数?它应该传递给每个动作,然后传递给每个视图和每个链接.
示例情况:我有实体A及其控制器.此外,我有另一个依赖于A的实体B.我需要经常访问"父"A实体,所以我想让它仍然像
http://some_url/b_controller/b_action?a_entity=xyz
Ben*_*ret 10
您应该能够从你的控制器做的一切,使用的组合before_filter和default_url_options:
class MyController < ApplicationController
before_filter :set_a_entity
def set_a_entity
@a_entity = params['a_entity']
# or @a_entity = Entity.find(params['a_entity'])
end
# Rails 3
def url_options
{:a_entity => @a_entity}.merge(super)
end
# Rails 2
def default_url_options
{:a_entity => @entity}
end
end
Run Code Online (Sandbox Code Playgroud)
这并没有解决设置初始值的问题@a_entity,但这可以从任何地方(视图,控制器等)完成.
如果你想在多个控制器中传递这个参数,你可以替换MyController < ApplicationController它ApplicationController < ActionController::Base,它也应该工作.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
2944 次 |
| 最近记录: |