如何使用 link_to 以便将传递给控制器​​的所有参数保留在 Ruby on Rails 2 中?

发布于 2024-10-17 15:39:17 字数 1794 浏览 1 评论 0原文

我有一个双语网站,其中有很好的 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 参数丢失了。

  1. 我在 /order-jira-with-greenhopper-hosting/11.html
    (:option => 11, :locale => 'en', :greenhopper => true)
  2. 生成的用于切换语言的链接为 /order-jira-hosting/11.html/zamow-hosting-jira/11.html
    (:option => 11, :locale => 'pl' and 'en', :greenhopper => false)...
  3. ...但它们应该是 /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.

  1. I am at /order-jira-with-greenhopper-hosting/11.html
    (:option => 11, :locale => 'en', :greenhopper => true)
  2. 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)...
  3. ...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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

豆芽 2024-10-24 15:39:17

您可以将发送到 link_to 的哈希值基于 params 哈希值,如果您按原样将其传递到 link_to 中,则会重新加载当前页面。您可以使用 Hash.merge(other_hash) 重置每个链接的 :locale 键:

<%= link_to '<polish image />', params.merge({:locale => 'pl'}), :class => 'a' %>

现在,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 into link_to as-is, would reload the current page. You can use Hash.merge(other_hash) to reset the :locale key for each link:

<%= link_to '<polish image />', params.merge({:locale => 'pl'}), :class => 'a' %>

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 via params.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文