[英]How to remove the action argument from Rails link_to function?
Not sure how to frame this so here goes.... 不知道该如何构架,所以在这里...。
I have the following link_to tag: 我有以下link_to标签:
<%= 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: 以及我的route.rb文件中的以下自定义路由:
map.connect ':controller/:action/:title/search_item/:id', :controller=>'search', :action=>'for_sale'
which generates a very nice SEO friendly URL: 生成一个非常好的SEO友好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: 如何从两者中删除:action参数,问题是当我取出:action选项并将link_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: 生成的URL不再是SEO友好的,而且非常难看:
/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. 我的自定义路由将重定向到控制器中的正确操作,因此不需要在URL中包含操作选项。 Anytime I remove or rename the :action option to something else - the URL gets "distorted", do you know how I can do this?
每当我删除:action选项或将其重命名为其他名称时-URL被“扭曲”,您知道我该怎么做吗?
Been trying a number of options but nothing seems to work. 尝试了多种选择,但似乎无济于事。
Thanks! 谢谢!
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
首先,您的路线
':controller/:title/search_item/:id'
没有:search_term
参数,但您尝试将其传递给link_to :search_term => search_term
Secondly, if you are always using controller 'search'
, you do not need to pass it as a parameter. 其次,如果您始终使用控制器
'search'
,则无需将其作为参数传递。
So, your route probably becomes 'search/:title/:search_term/:id'
因此,您的路线可能变为
'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)
您可以创建不带id的网址:
search_path(listing.title, search_term)
或不带id和search_term的网址: search_path(listing.title)
If you want to make only :search_term
optional, put in at the end of the route: 如果只想使
:search_term
可选,请放在路由的末尾:
map.search 'search/:title/:id/:search_term', :controller=>'search', :action=>'for_sale', :search_term => nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.