views:

18

answers:

1

I have a nice RESTful controller in my Rails application that uses responds_to to distinguish between requests for HTML and CSV requests, serving the HTML if one uses the URL normally, and the CSV if one adds ".csv" to the end of the URL.

In my view layer ERB file, I want to have a link to the CSV. I could just get the URL for the appropriate action normally and then just add ".csv" to the end of it, but that seems kinda hacky. I don't, however, see anthing in the url_for docs that would allow me to do this more cleanly.

Is there something I'm missing, or am I just stuck with something like this?

<a href="<%= url_for @topic%>.csv">Download CSV</a>

Thanks for any insight!

+1  A: 
url_for @topic, :format => :csv

is probably what you are looking for

Jed Schneider
Indeed, that appears to do the trick beautifully! Thank you. Is it documented somewhere? I sure don't see it under the url_for docs, and want to be sure I know where to look for stuff like this!
Sean McMains
I like using apidock. users can add comments to the documentation.http://apidock.com/rails/ActionController/Base/url_for
Jed Schneider
That isn't working for me. What version of Rails are you using?
Nicolas Buduroi