禁用 authenticity_token

对于这个问题,google了好长时间,终于找到答案,

No modifications to work with CSRF protection were applied to the in_place_editing plugin, as a result it doesn't work at all with CSRF protection turned on.

Also, scriptaculous/prototype have moved away from the evalScripts option for Ajax.Request, the option is now htmlResponse (htmlResponse should be the opposite of the old evalScripts). As a result at the moment for some requests the RJS does not get evaluated and instead is inserted into the page as escaped text.

Attached is a patch that fixes both these issues in the plugin, including additional tests.

Also changed: added the :text_between_controls option available in modern scriptaculous, for rendering things like <input type="submit" value="Save"/> or <a href="whatever">cancel</a>.


lib/in_place_macros_helper.rb

oldnew
4040function << "'#{url_for(options[:url])}'"
4141
4242js_options = {}
43
44if protect_against_forgery?
45options[:with] ||= "Form.serialize(form)"
46options[:with] += " + '&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')"
47end
48
4349js_options['cancelText'] = %('#{options[:cancel_text]}') if options[:cancel_text]
4450js_options['okText'] = %('#{options[:save_text]}') if options[:save_text]
4551js_options['loadingText'] = %('#{options[:loading_text]}') if options[:loading_text]
5056js_options['externalControl'] = "'#{options[:external_control]}'" if options[:external_control]
5157js_options['loadTextURL'] = "'#{url_for(options[:load_text_url])}'" if options[:load_text_url]
5258js_options['ajaxOptions'] = options[:options] if options[:options]
53js_options['evalScripts'] = options[:script] if options[:script]
59js_options['htmlResponse'] = !options[:script] if options[:script]
5460js_options['callback'] = "function(form) { return #{options[:with]} }" if options[:with]
5561js_options['clickToEditText'] = %('#{options[:click_to_edit_text]}') if options[:click_to_edit_text]
62js_options['textBetweenControls'] = %('#{options[:text_between_controls]}') if options[:text_between_controls]
5663function << (', ' + options_for_javascript(js_options)) unless js_options.empty?
5764
5865function << ')'

说是rails2.0以后,内置了一个防止CSRF (Cross-Site Request Forgery)攻击的功能,protect_from_forgery 方法。

解决办法是在in_place_macros_helper.rb 文件中加了一段代码:

if protect_against_forgery?
options[:with] ||= "Form.serialize(form)"
options[:with] += " + '&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')"
end

删除
js_options[' evalScripts'] = options[:script] if options[:script]
添加
js_options[' htmlResponse'] = !options[:script] if options[:script]
js_options['textBetweenControls'] = %('#{options[:text_between_controls]}') if options[:text_between_controls]

引用:http://dev.rubyonrails.org/ticket/10055
http://dev.rubyonrails.org/attachment/ticket/10055/in_place_editing_should_work_with_csrf_and_rjs.patch


另一解决方法:(不安全)
由于我的rails是从rails1。0升级到2。1。2的,所以在test环境中也会使用Request forgery protection(防止csrf)攻击功能,所以在environment.rb文件中设置如下,可取消保护
config.action_controller.allow_forgery_protection = false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值