Em 12-05-2012 02:36, Michael Koziarski escreveu:
On 12/05/2012, at 5:58 AM, Rodrigo Rosenfeld Rosas <[email protected] <mailto:[email protected]>> wrote:

Today I had a strange behavior that made me suspect of jQuery at first, but then it happened that I've faced two gotchas, one from CoffeeScript and one from Rails itself.

I have something like this:

routes.rb
post '/fields/:id.:format' => 'fields#show', as: :field, constraints: {id: /\d+/}
    post '/fields/remove/:id' => 'fields#remove', as: :remove_field


If you look at mapper.rb you'll find the answer

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb

Basically, :format is appended to every route unless you explicitly opt out, I believe this started with 3.0?


Thanks, Michael, I've already found out the answer, but after having a surprise first :)

What I was suggesting is that we changed the generated routes.rb so that the Rails 1 style route could be written as

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

As well as mentioning and explaining the "format" option, like some of these examples:

post '/products/:id.:format' => 'products#show', as: :field, constraints: {id: /\d+/} # format is required post '/products/:id' => 'products#show', format: true, as: :field, constraints: {id: /\d+/} # this is equivalent post '/products/:id.json' => 'products#show', format: :json # it only answers to json format post '/products/:id' => 'products#show', format: :json # the same with a shorter URL but still allowing you to cache the result and use the correct content-type

But then I decided to test it and I was surprised that it doesn't make any difference if you use caches_page.

Also, there are more weird rules with regards to page or action caching. The routes rules don't seem to be respected if you use caching.

Consider this example:

get '/products' => 'products#list', format: :json

try to GET /products with caches_page enabled and you won't get a 'application/json' content-type in the next responses because it won't probably even read the routes rules.

Also, if you GET /products.html not only it accepts this request but the content-type will be 'text/html' instead of 'application/json' (independent from caching).

Also, consider this other example:

get '/products.json' => 'products#list', format: :json

This will also respond to '/products.json.html' for example, and I don't think it should answer to anything else but '/products.json'. Also, the content-type declared in the routes wouldn't be respected.


Maybe I'm the only one considering the current behavior an issue, but I'd like to ask you to consider some changes in the routing rules with regards to format to make them less loose.

What is your opinion on the subject?

Best,
Rodrigo.

--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to