帮助程序中的Rails Form块-如何包括“防止伪造" [英] Rails Form block in helper - How do i include "Protect from forgery"
问题描述
我正在尝试为我的流动主题语言构建一个表单块.我的方法基于此答案 .答案似乎是不完整的.
I'm trying to build a form block for my liquid theme language. I have based my approach on this answer. How ever the answer seems to be incomplete.
问题是无法防止伪造和其他一些方法.导致错误:
The problem is that protect from forgery and some other methods are unavailable. Causing an error:
Liquid error: undefined method `protect_against_forgery?' for #
这是我的代码:
class LiquidFormTag < Liquid::Block
include ActionView::Context
include ActionView::Helpers::FormHelper
def initialize(tag_name, markup, tokens)
super
end
def render(context)
form_tag("#") do
super
end
end
end
Liquid::Template.register_tag('liquid_form', LiquidFormTag)
有人知道我如何添加protect_against_forgery
方法吗?
Does any one know how i add the protect_against_forgery
method do this class?
编辑:这是错误输出:
this is the error output:
这是我的Liquid代码的相关部分:
This is the relevant part of my Liquid code:
{% ticket_form %}
{% for offer in event.offers %}
<div class="well well-sm">
<div class="row">
<div class="col-xs-3 col-sm-5 col-md-6 col-lg-7">
<h5>{{offer.name}}</h5>
</div>
<div class="col-xs-9 col-sm-7 col-md-6 col-lg-5 pull-right">
<div class="input-group">
<span class="input-group-addon">{{offer.price}}</span>
<input type="email" class="form-control tickets-count" cols="2" id="exampleInputEmail1" placeholder="0">
<span class="input-group-btn">
<button type="button" class="btn btn-default"><i class="fa fa-plus"></i></button>
<button type="button" class="btn btn-default"><i class="fa fa-minus"></i></button>
</span>
</div>
</div>
</div>
</div>
{% endfor %}
{% endticket_form %}
推荐答案
我同意Rodrigo,但是很难确定将被释放给控制器的方法名称.
I agree to Rodrigo but it will be hard to identify method names that will be deleagated to controller.
这就是为什么我更喜欢扩展Liquid::Block
类并在响应时将缺少的方法委托给控制器的原因.
That's why I prefer to extend Liquid::Block
class and delegate missing methods to controller if responds..
class LiquidFormTag < Liquid::Block
include ActionView::Context
include ActionView::Helpers::FormHelper
attr_reader :controller
def initialize(tag_name, markup, tokens)
super
end
def render(context)
@controller = context.registers[:controller]
form_tag('#') do
super(context).html_safe
end
end
end
Liquid::Block.class_eval do
# This delegates missing - including private & protected - methods (like protect_against_forgery?) to controller.
def method_missing(*args)
begin
if controller.respond_to?(args.first, true)
controller.send(args.first)
else
super
end
rescue
super
end
end
end
这篇关于帮助程序中的Rails Form块-如何包括“防止伪造"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!