如何在Rails中的JSON / XML请求过滤器之前跳过 [英] How to skip before filters for json/xml requests in rails

查看:111
本文介绍了如何在Rails中的JSON / XML请求过滤器之前跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一种基于格式跳过过滤器的方法,如下所示,但是我想知道是否有更好的方法,因为这会使事情变得混乱并且不是很干燥。

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

如果我不这样做,则由于current_cart方法而导致json请求失败,

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

推荐答案

您可以这样做:

before_filter :current_cart, :unless => :format_js?

def format_js?
  request.format.js?
end

希望这会有所帮助。

这篇关于如何在Rails中的JSON / XML请求过滤器之前跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆