[AWDwR4]13章出错 protect_against_forgery

 13.2,Iteration H2: Integration Testing of Applications,写完测试,运行 rake test出错:

复制代码
  1) Error:
test_order_shipped(NotifierTest):
ActionView::Template::Error: undefined method `protect_against_forgery?' for #<#<Class:0x00000004bcbca0>:0x00000004d43c40>
    /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/url_helper.rb:340:in `button_to'    
复制代码

    看提示,是在 _line_item.html.erb中的button_to出错了,以为是其中的变量line_item为空,利用logger.info("line_item: #{line_item.inspect}")打印到日志中,发现有值,不为空。
    搜索这个问题时,看到这个地方,把button_to去了就可以,思路跟12章play time中的第二个问题一样的,通过@order.nil 来区分需不需要显示这个按钮。这个按钮是前面某一章的Play time里面添加的。
  修改 app/views/line_items/_line_item.html.erb如下:

<% if  @order . nil ? %>
         <td><%= button_to "Decrease" , line_item, :remote  => true , :method => :delete , :confirm  => "Are you sure?"
%></td>
         <% end  %>

  这样虽然测试搞定了,但是不爽啊,因为没有搞明白为什么会出这个错。
  后来看到这个帖子,回复中提到了Rails 程序为了防止 CSRF /XSS 攻击,在使用form_for 和form_tag建立forms时,会在里面加上一个token order_shipped.html.erb模板会调用 render(line_item),而_line_item.html.erb模板中使用了 button_to, 这个函数会创建一个form元素。这在email中不能正常工作,mailer类不能正常处理它。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值