action_controller/caching/actions.rb normalize! breaks because it calls URI.parser.escape with a symbol #10

Closed
mobilutz opened this Issue Jan 2, 2014 · 9 comments

Projects

None yet

4 participants

@mobilutz
mobilutz commented Jan 2, 2014

I get this error:
NoMethodError (undefined method gsub' for :json:Symbol): /usr/local/var/rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/uri/common.rb:304:inescape'
vendor/bundle/ruby/2.0.0/gems/actionpack-action_caching-1.1.0/lib/action_controller/caching/actions.rb:202:in normalize!' vendor/bundle/ruby/2.0.0/gems/actionpack-action_caching-1.1.0/lib/action_controller/caching/actions.rb:197:ininitialize'
vendor/bundle/ruby/2.0.0/gems/actionpack-action_caching-1.1.0/lib/action_controller/caching/actions.rb:165:in `new'

When I add a ".to_s" to the URI.parser.escape call in line 202 it works.

Could you maybe fix this? Thanks

@pixeltrix
Member

The error you're getting implies that controller.params[:format] is :json - are you setting this yourself somewhere?

@mobilutz
mobilutz commented Jan 2, 2014

I don't specifically set the controller param. In my routes.rb I have the following:

namespace :api, :defaults => {:format => :json} do
...
end
@pixeltrix
Member

And what does your caches_action definition look like?

@mobilutz
mobilutz commented Jan 3, 2014

This is in my controller:

caches_action :init, :cache_path => :caching_path.to_proc, :expires_in => 1.hour
...
def caching_path
  "#{request.path[1..-1].gsub('.json','')}"
end

Although the action caching_path never gets called because the error happens before that.

@pixeltrix pixeltrix closed this in 782bf07 Jan 4, 2014
@pixeltrix
Member

Thanks for that - I was just trying to understand why you've triggered the error when so many others haven't. It turns out that caching_path is called before the error happens but because you're removing the extension and returning a string the url_for just returns your string and then normalize_path tries to put it back on. I'm not sure why you want to remove the format from the path only for it to be added back on but I've fixed it anyway since there's an outstanding PR against Rails to standardise on symbols for the :format parameter, so other people may run into it.

@mobilutz
mobilutz commented Jan 5, 2014

Thank you very much for the quick fix. I don't think that returning a string for caching_path has anything to do with that, because the function never gets called. The error happens before the function - tested with binding. I think that the "problem" is that I use a symbol as format in my routes.rb contrary to the Rails doc where they use a string: http://guides.rubyonrails.org/routing.html#defining-defaults

@pixeltrix
Member

Not that it matters but your :cache_path proc is called here, before the ActionCachePath is instantiated here which then generates the error.

@elbarto132

i have the same problem, but all i do is setting caches_action :index, :show in the controller, no path or anything
NoMethodError - undefined method gsub' for :json:Symbol: /Users/elbarto132/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/uri/common.rb:304:inescape'
actionpack-action_caching (1.1.1) lib/action_controller/caching/actions.rb:202:in normalize!' actionpack-action_caching (1.1.1) lib/action_controller/caching/actions.rb:197:ininitialize'
actionpack-action_caching (1.1.1) lib/action_controller/caching/actions.rb:165:in new' actionpack-action_caching (1.1.1) lib/action_controller/caching/actions.rb:165:inaround'

as mobilutz mentioned already, this happens if you set the default format in routes as a hash (maybe only for json), setting it as a string solves the problem.

@mattymess

Is this ever going to make its way into rubygems so I don't have to point my gemfile to the repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment