开发者

Rails link_to not automagically using request.format as extension in links

开发者 https://www.devze.com 2023-01-15 00:14 出处:网络
I\'m setting request.format = :mobile within an ApplicationController before_filter. before_filter :some_filter

I'm setting request.format = :mobile within an ApplicationController before_filter.

before_filter :some_filter
def some_filter
   request.format = :mobile
en开发者_如何学JAVAd

I have this mapping in routes:

map.my_list '/my_list.:format', :controller => "of_no", :action => "significance"

When I do the following:

<%= link_to "My List", my_list_path %>

I get the following (ACTUAL below):

<a href="/my_list">My List</a> <!-- THIS IS THE PROBLEM -->

I want this to be (EXPECTED below):

<a href="/my_list.mobile">My List</a> <!-- THIS IS THE EXPECTED -->

However, if I do the following:

<%= link_to "My List", my_list_path(:format => "mobile") %> 

Then I get the EXPECTED result, but doing this to every "link_to" is not a viable solution.

Any idea?

Thank you


You should only need to specify the format in the helper if you want the URL to show my_list.mobile. However, if your filter doesn't use the URL to determine the format, and you're setting the format in your controller, why would you need it in the URL too? Your controller should respond to the .mobile format without it in the URL when request.format is overridden.

You could probably get away with removing the .:format part of your route definition and just rely on the request object being set.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号