[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类不能正常处理它。

回到本系列的目录      













posted on 2012-11-21 22:07  生栋  阅读(371)  评论(0编辑  收藏  举报

导航