11

I'm moving a project from rails 3.1 to rails 3.2.2 and I have this:

= link_to 'CSV', :action => 'list', :search => @search, :format => 'csv'

In rails 3.1 this specifies the format in the html link (format=csv) and it is caught by a respond_with, but in 3.2.2 the format never makes it into the link. I scanned through the list of commits on github and can't find anything that relates to this.

Edit:

Looks like this is an issue with url_for

#rails 3.1
url_for :controller=>'posts', :action=>'index', :format=>:xml
/admin/posts/index?format=xml

#rails 3.2.2
url_for :controller=>'posts', :action=>'index', :format=>:xml
/admin/posts/index

#rails 3.2.2
url_for :controller=>'posts', :action=>'index', :format=>:xml, :id => 5
/admin/posts/index/5.xml
Cœur
  • 37,241
  • 25
  • 195
  • 267
cbron
  • 4,036
  • 3
  • 33
  • 40
  • Did you ever get around this? I'm experiencing the same thing. Only with named routes I could do something like `<%= link_to "something", something_path(something, :format => :xml) %>` – slhck Jul 11 '12 at 20:54
  • Yea I ended up using a hack similar to what you have. I don't remember exactly but I think I physically put the location in the link like `:action => 'index.xml'`, that doesn't feel right though. – cbron Jul 12 '12 at 16:30

2 Answers2

13

Try using :format => :csv

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to#32-Link-to-same-URL-with-different-format

Matzi
  • 13,770
  • 4
  • 33
  • 50
  • Nope. I also tried using a resource path: `documents_path(:format => :csv)` but that sends me to documents.csv instead of adding it as a param. – cbron May 02 '12 at 20:24
  • 1
    `link_to 'cvs', :action => 'show', :format => :csv` I tried it now, it works for me perfectly. – Matzi May 02 '12 at 20:28
  • I just tried that exact link in another controller and its still not working, but its adding the &format=xml for you ? Weird. – cbron May 02 '12 at 20:36
0

I have run into the same problem while upgrading from Rails 3.0 to 3.2.17.

From what I see, the problem was not (as the other answers suggest) about the way parameters to link_to were specified, but had to do with the definition of the routes in routes.rb. It looks like in 3.2, the :format parameter can only be passed through as a URL suffix. If there is no route that maps :formatto the URL, then it will be ignored by link_to. 3.0 would have added format as an HTTP parameter, in such a case. 3.2 doesn't do that anymore.

My solution was to change my original default route from

match ':controller(/:action(/:id(.:format)))'

into

match ':controller(/:action(/:id)(.:format))'

The original definition covered URLs like /admin/posts/index/5.xml, but not /admin/posts/index.xml. This looks like the same symptom as in the original question here.

After I applied the change, the :format was also included into URLs that didn't have an id in them.

sys64738
  • 41
  • 6