Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip before filters for json/xml requests in rails

I found a way to skip before filters based on the format, as seen below, but I'm wondering if there is a better way since this clutters things and isn't very DRY.

before_filter do |controller|
  :current_cart unless controller.request.format.js?
end

If I don't do this, json requests fail because the current_cart method, and other methods, do things only meant for html.

like image 820
Shagymoe Avatar asked Jul 02 '10 17:07

Shagymoe


1 Answers

You could do it this way:

before_filter :current_cart, :unless => :format_js?

def format_js?
  request.format.js?
end

Hope this helps.

like image 197
Geoff Lanotte Avatar answered Oct 12 '22 16:10

Geoff Lanotte