如何从 Rails link_to 函数中删除操作参数?
不知道如何构建这个,所以这里......
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的路线
':controller/:title/search_item/:id'
没有:search_term
参数,但您尝试将其传递到 link_to:search_term => search_term
其次,如果您始终使用控制器
'search'
,则不需要将其作为参数传递。因此,您的路线可能会变成
'search/:title/:search_term/:id'
另外,请尝试使用命名路线:
编辑: 可选参数
您可以为参数提供默认值在路线的尽头。
因此,如果您设置
You can create url without id:
search_path(listing.title, search_term)
或者没有 id 和 search_term:
search_path(listing.title)
如果您只想使
:search_term
可选,请在路线末尾输入: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:
Edit: optional parameters
You can supply default value for parameters at the end of the route.
So, if you set
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: