如何从 Rails link_to 函数中删除操作参数?

发布于 2024-08-30 15:15:21 字数 1164 浏览 8 评论 0原文

不知道如何构建这个,所以这里......

我有以下 link_to 标签:

<%= link_to("My test title",{:controller=>"search", :action=>"for-sale", :title => listing.title, :search_term => search_term, :id=> listing.id}) %>

以及在我的 paths.rb 文件中的以下自定义路由:

map.connect ':controller/:action/:title/search_item/:id', :controller=>'search', :action=>'for_sale' 

它生成一个非常好的 SEO 友好的 URL:

/search/for-sale/sometitle/searchterm/123456

如何删除 :action 参数两者,问题是当我取出 :action 选项 & 时将我的 link_to 标记更改为:

<%= link_to("My test title",{:controller=>"search", :title => listing.title, :search_term => search_term, :id=> listing.id}) %>

将我的自定义路由更改为:

map.connect ':controller/:title/search_item/:id', :controller=>'search', :action=>'for_sale' 

生成的 URL 不再对 SEO 友好且非常丑陋:

/search?title=test&search_term=test&id=1141409

我的自定义路由重定向到控制器内的正确操作,因此不需要在 URL 中添加操作选项。每当我删除 :action 选项或将其重命名为其他选项时 - URL 就会“扭曲”,你知道我该怎么做吗?

尝试了多种选择,但似乎没有任何效果。

谢谢!

Not sure how to frame this so here goes....

I have the following link_to tag:

<%= link_to("My test title",{:controller=>"search", :action=>"for-sale", :title => listing.title, :search_term => search_term, :id=> listing.id}) %>

and the following custom route in my routes.rb file:

map.connect ':controller/:action/:title/search_item/:id', :controller=>'search', :action=>'for_sale' 

which generates a very nice SEO friendly URL:

/search/for-sale/sometitle/searchterm/123456

How can I remove the :action param from both, the problem is when I take out the :action option & change my link_to tag to:

<%= link_to("My test title",{:controller=>"search", :title => listing.title, :search_term => search_term, :id=> listing.id}) %>

and my custom route to:

map.connect ':controller/:title/search_item/:id', :controller=>'search', :action=>'for_sale' 

The URL generated is no longer SEO friendly and very ugly:

/search?title=test&search_term=test&id=1141409

My custom route is redirecting to the correct action within the controller so there is no need for action option to be in the URL. Anytime I remove or rename the :action option to something else - the URL gets "distorted", do you know how I can do this?

Been trying a number of options but nothing seems to work.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

那一片橙海, 2024-09-06 15:15:21

首先,您的路线 ':controller/:title/search_item/:id' 没有 :search_term 参数,但您尝试将其传递到 link_to :search_term => search_term

其次,如果您始终使用控制器'search',则不需要将其作为参数传递。

因此,您的路线可能会变成 'search/:title/:search_term/:id'

另外,请尝试使用命名路线:

map.search 'search/:title/:search_term/:id', :controller=>'search', :action=>'for_sale'

<%= link_to("My test title", search_path(listing.title, search_term, listing.id)) %>

编辑: 可选参数

您可以为参数提供默认值在路线的尽头。

因此,如果您设置

map.search 'search/:title/:search_term/:id', :controller=>'search', :action=>'for_sale', :id => nil, :search_term => nil

You can create url without id: search_path(listing.title, search_term)
或者没有 id 和 search_term: search_path(listing.title)

如果您只想使 :search_term 可选,请在路线末尾输入:

map.search 'search/:title/:id/:search_term', :controller=>'search', :action=>'for_sale', :search_term => nil

Firstly, your route ':controller/:title/search_item/:id' doesn't have :search_term parameter, but you are trying to pass it in link_to :search_term => search_term

Secondly, if you are always using controller 'search', you do not need to pass it as a parameter.

So, your route probably becomes 'search/:title/:search_term/:id'

Also, try using named routes:

map.search 'search/:title/:search_term/:id', :controller=>'search', :action=>'for_sale'

<%= link_to("My test title", search_path(listing.title, search_term, listing.id)) %>

Edit: optional parameters

You can supply default value for parameters at the end of the route.

So, if you set

map.search 'search/:title/:search_term/:id', :controller=>'search', :action=>'for_sale', :id => nil, :search_term => nil

You can create url without id: search_path(listing.title, search_term)
Or without both id and search_term: search_path(listing.title)

If you want to make only :search_term optional, put in at the end of the route:

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