On 13 May 2012, at 14:16, Rodrigo Rosenfeld Rosas wrote:

> I was not saying that it wasn't possible to write the proper routes, just 
> that they don't seem very consistent.

I'm not getting why you think they're inconsistent - there's always a optional 
format parameter unless you tell it not to add one or you make it non-optional 
in the route itself.

> I would guess that most applications won't be API ones, so many of them won't 
> opt for REST and responds_to.

There are plenty of non-API apps that utilise REST and responds_to

> For example, all applications I've been working with for the past decade will 
> only need to respond to a single format per action.
> 
> That means that if I wanted strict rules to my routes I'd have to write them 
> as:
> 
> get '/anything', format: false
> 
> or
> 
> get '/anything.:format' => 'anything#index', constraints: {format: /json/}, 
> defaults: {format: :json}
> 
> I could use a block for this common option except that not all of my actions 
> will return a JSON.

Just use a scope - :format works here as well:

scope :format => false do
  resources :products
end

You could even use a nested scope to wrap your actions that return JSON to add 
the constraints/defaults.

> Today is Mother's day in Brazil and I'm going to leave in a few minutes to 
> have lunch with my mom in a near city, but I guess I've already found some 
> bugs.

<example code removed>

> I've made several experiments that yield to strange results when it comes to 
> content-type.
> 
> Basically, I'm getting 'text/html' instead of 'application/json'. This 
> happens even if I pass content_type: 'application/json' explicitly.

The test/expire route needs to be set up in the same way - this works for me:

scope :format => false do
  defaults :format => 'json' do
    get 'test/index', :as => :index
    get 'test/expire', :as => :expire
  end
end

> Another bug is that expire_action doesn't seem to work correctly either. For 
> example, if you use the commented route the output will be:
> 
> Expire fragment views/localhost:3000/assets?controller=test

This is the route you get if you have the asset pipeline enabled and none of 
your applications route's matches.

> It doesn't matter if I use a named route or "expire_action action: :index" or 
> "expire_action action: :index, format: :json" if I remember correctly.

There's a slight wrinkle here - read and write action caches always adds the 
:format so your cache file ends with .json but expire_action is essentially a 
wrapper around url_for so gives a path without the :format, making the expiry 
fail. I'll look at whether this needs addressing. In the meantime it's probably 
best to use formatted urls - these routes work fine:

constraints :format => 'json' do
  get 'test/index', :as => :index
  get 'test/expire', :as => :expire
end

You'll need to pass the format to the expire_action as it doesn't infer it from 
the current request - otherwise you could only expire JSON caches from JSON 
requests.

Hope you had a good Sunday.


Andrew White


-- 
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