0

I already see some similar quetions:

How to set the default format for a route in Rails?

Rails 3 respond_to: default format?

Rails 3.1 force .html instead of no extension

But any solution didn't work in rails 4 for me:

in routers.rb

 :defaults => { :format => 'html' }

in app/controllers/some_controller.rb

before_filter :set_format

def set_format
  request.format = 'html'
end

in config/application.rb

config.default_url_options = { :format => "html" }

Any of there. I tried all it together and each separate.

Here my code:

link_to("Read more", article_path(article, :format => "html"))
# => <a href="/1.html">Read more</a> 

What will be if I remove :format => "html":

link_to("Read more", article_path(article))
# => <a href="/1">Read more</a> 

What I am want:

link_to("Read more", article_path(article))
# => <a href="/1.html">Read more</a> 

Share your suggestion for it, please.

4
  • 2
    I see that default formal is xml, and you need to override it as html, so why you don't want to write :format => "html" every time ? Aug 19, 2014 at 12:51
  • Why do you force request format to xml? Aug 19, 2014 at 13:12
  • @mohamed-yakot, thanks, correct question. It was a copypaste mistake Aug 19, 2014 at 13:54
  • @michal-szyndel, thanks to you too. fix question Aug 19, 2014 at 13:55

1 Answer 1

1

If you use add these options to your route, I think you'll end up with the .html in the path like you want. You shouldn't even need to modify your call to article_path.

format: true, defaults: { format: 'html' }

I ran into something similar. Looking through the Rails routing docs helped me find an answer.

I have an action which I want to default to the CSV format, but I also want to generate the URL with the .csv in it. This way the client can tell, just by looking at the URL, that it's a CSV. Without the .csv, it's not very clear what the link will be.

My route was something like this:

get 'customers/with_favorite_color/:color' => 'customers#with_favorite_color', as: :customers_with_favorite_color, format: 'csv'

And I was generating the link like this:

customers_with_favorite_color_url(color: 'blue', format: :csv)
# => `http://localhost:3000/customers/with_favorite_color/blue

Except, I want http://localhost:3000/customers/with_favorite_color/blue.csv.

It seemed that the format would not be added to the URL if that format was also specified in the route.

To get this to work, I changed my route to

get 'customers/with_favorite_color/:color' => 'customers#with_favorite_color', as: :customers_with_favorite_color, format: true, defaults: { format: 'csv' }

Notice the options added/changed are: format: true, defaults: { format: 'csv' }

And now, when I generate the URL, I get the .csv appended like I wanted.

customers_with_favorite_color_url(color: 'blue')
# => `http://localhost:3000/customers/with_favorite_color/blue.csv

Update

To apply this to many routes, you can follow the advice of How to set default format for routing in Rails? and use a scope with these defaults.

scope format: true, defaults: { format: 'html' } do
  # Define your resources here like normal
end
3
  • thanks for the answer. I'd mark it as a right one. Only one single question: do you find the way to define it (format: true, defaults: { format: 'html' }) globally? I mean for the whole project? Apr 13, 2016 at 9:59
  • I believe you can define this globally by following the advice here: stackoverflow.com/q/21016987/249218
    – Kyle Tolle
    Apr 13, 2016 at 22:09
  • I updated my answer with that additional information.
    – Kyle Tolle
    Apr 13, 2016 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.