[英]How to check if method is executed via a yield/block.call?
In a very basic example I have two Rails form helpers toggles
and checkbox
. 在一个非常基本的示例中,我有两个Rails表单帮助器
toggles
和checkbox
。 I want the markup for the checkboxes different when they are inside the toggles block. 我希望复选框在切换块内时标记的标记不同。 I tried to use a instance variable to check if I'm inside the
toggles
method but someone said maintaining state like this would break the ability for this to be used in multithreaded applications. 我试图使用一个实例变量来检查我是否在
toggles
方法内,但是有人说保持这种状态将破坏在多线程应用程序中使用它的能力。 What would be a better way? 有什么更好的方法?
So basically I want to check if I'm inside toggles
when checkbox
is executed. 因此,基本上,我想检查在执行
checkbox
是否处于toggles
状态。
<%= toggles 'Select one' do %>
<%= f.checkbox 'Foo' %>
<%= f.checkbox 'Bar' %>
<%= f.checkbox 'Baz' %>
<% end %>
def toggles(*args)
@inside_toggles = true
template.concat yield
@inside_toggles = false
end
def checkbox(*args)
if @inside_toggles
...
else
...
end
end
If it's only a formatting issue, did you try using CSS? 如果仅是格式问题,您是否尝试过使用CSS?
You add a class for helper method toggles
您添加一个用于辅助方法
toggles
的类
def toggles
content_tag :div, class: 'toggle'
end
Then in your CSS 然后在您的CSS中
.toggle input[type="checkbox"] {
/* special format */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.