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.

Community
  • 1
  • 1
goodniceweb
  • 167
  • 1
  • 10

1 Answers1

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
Community
  • 1
  • 1
Kyle Tolle
  • 694
  • 2
  • 7
  • 17
  • 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? – goodniceweb Apr 13 '16 at 09:59
  • I believe you can define this globally by following the advice here: http://stackoverflow.com/q/21016987/249218 – Kyle Tolle Apr 13 '16 at 22:09
  • I updated my answer with that additional information. – Kyle Tolle Apr 13 '16 at 22:13