如何使用 link_to 以便将传递给控制器的所有参数保留在 Ruby on Rails 2 中?
我有一个双语网站,其中有很好的 SEO 网址。使用 Ruby on Rails 2.3.10。
routes.rb
片段:
map.connect 'order-jira-hosting/:option.html',
:controller => 'order', :action => 'index', :locale => 'en'
map.connect 'order-jira-with-greenhopper-hosting/:option.html',
:controller => 'order', :action => 'index', :locale => 'en', :greenhopper => true
map.connect 'zamow-hosting-jira/:option.html',
:controller => 'order', :action => 'index', :locale => 'pl'
map.connect 'zamow-hosting-jira-z-greenhopper/:option.html',
:controller => 'order', :action => 'index', :locale => 'pl', :greenhopper => true
如您所见,:locale
和 :greenhopper
“隐藏”在 URL 中。
有一个开关,您可以更改当前页面的语言。请参阅我的 views/layouts/default.erb
:
<%= link_to image_tag('icons/polish.png', :alt => 'polski'), { :locale => 'pl'}, :class => 'a' %>
<%= link_to image_tag('icons/english.png', :alt => 'English'), { :locale => 'en'}, :class => 'a' %>
我只是没有指定控制器和操作,以便我被重定向到具有不同区域设置的当前控制器和操作。不幸的是,:greenhopper 参数丢失了。
- 我在
/order-jira-with-greenhopper-hosting/11.html
(:option => 11, :locale => 'en', :greenhopper => true
) - 生成的用于切换语言的链接为
/order-jira-hosting/11.html
和/zamow-hosting-jira/11.html
(:option => 11, :locale => 'pl' and 'en', :greenhopper => false)
... - ...但它们应该是
/order -jira-with-greenhopper-hosting/11.html
和/zamow-hosting-jira-z-greenhopper/11.html
(:option => 11, :locale => 'pl' and 'en', :greenhopper => true)
如何使用 link_to 方法以便保留传递给控制器的所有参数?感谢您的帮助。
I have a bilingual site with nice URLs for SEO. Using Ruby on Rails 2.3.10.
routes.rb
fragment:
map.connect 'order-jira-hosting/:option.html',
:controller => 'order', :action => 'index', :locale => 'en'
map.connect 'order-jira-with-greenhopper-hosting/:option.html',
:controller => 'order', :action => 'index', :locale => 'en', :greenhopper => true
map.connect 'zamow-hosting-jira/:option.html',
:controller => 'order', :action => 'index', :locale => 'pl'
map.connect 'zamow-hosting-jira-z-greenhopper/:option.html',
:controller => 'order', :action => 'index', :locale => 'pl', :greenhopper => true
As you can see, :locale
and :greenhopper
are "hidden" in the URL.
There is a switch so that you can change the language of the current page. See my views/layouts/default.erb
:
<%= link_to image_tag('icons/polish.png', :alt => 'polski'), { :locale => 'pl'}, :class => 'a' %>
<%= link_to image_tag('icons/english.png', :alt => 'English'), { :locale => 'en'}, :class => 'a' %>
I simply don't specify a controller and action so that I am redirected to the current controller and action with different locale. Unfortunately, :greenhopper parameter gets lost.
- I am at
/order-jira-with-greenhopper-hosting/11.html
(:option => 11, :locale => 'en', :greenhopper => true
) - Generated links for switching languages are
/order-jira-hosting/11.html
and/zamow-hosting-jira/11.html
(:option => 11, :locale => 'pl' and 'en', :greenhopper => false)
... - ...But they should be
/order-jira-with-greenhopper-hosting/11.html
and/zamow-hosting-jira-z-greenhopper/11.html
(:option => 11, :locale => 'pl' and 'en', :greenhopper => true)
How to use link_to method so that all parameters passed to controller are preserved? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将发送到
link_to
的哈希值基于 params 哈希值,如果您按原样将其传递到link_to
中,则会重新加载当前页面。您可以使用Hash.merge(other_hash)
重置每个链接的:locale
键:现在,
params
可以 包含控制器和操作键,但它们是生成当前页面的控制器和操作,因此链接的行为应该就像页面刷新一样,只有通过params.merge
更改的参数会发生变化。希望这有帮助!
PS:如果您担心的话,
params.merge
不会更改 params 哈希 - 合并的结果将作为新哈希返回。You can base the hash you send to
link_to
off the params hash, which, if you passed it intolink_to
as-is, would reload the current page. You can useHash.merge(other_hash)
to reset the:locale
key for each link:Now,
params
does contain controller and action keys, but they're the controller and action that generated the current page, so the link should behave just like a page refresh, with only the parameters you changed viaparams.merge
changing.Hope this helps!
PS:
params.merge
doesn't change the params hash, if you're concerned about that - the result of the merge is returned as a new hash.