[英]How can i a surround multiple inputs from a form with a div tag?
I am currently working on a rails app that has a common pattern on fields throughout many forms and many fields of the same form.我目前正在开发一个 Rails 应用程序,该应用程序在许多 forms 和许多相同形式的字段中具有共同的模式。 This is the pattern:这是模式:
<%= simple_form_for(@evaluation) do |f| %>
<%= f.error_notification %>
<div class="toggle">
<div class="toggle_selector">
<%= f.input :was_answered, :as => :radio, :collection => [:yes, :no, '?'] %>
</div>
<div class="toggle_content">
<%= f.input :name %>
<%= f.input :answer %>
<%= f.input :score %>
</div>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
There is a main div tag that surrounds two other divs.有一个主 div 标签围绕着另外两个 div。 The toggle_content div is made visible or not, using javascript, based on the value from the input that is in toggle_selector div.使用 javascript,根据 toggle_selector div 中输入的值,使 toggle_content div 可见或不可见。
I tried to extract a couple helpers that would take the inputs as parametes and output the desired output:我试图提取几个将输入作为参数的助手和 output 所需的 output:
def toggling_field_for form, selector, &block
content_tag :div, :class => 'toggle' do
%{#{toggle_selector form, selector}
#{toggle_content &block if block_given?}
}.html_safe
end
end
private
def toggle_selector form, selector
content_tag :div, :class => 'toggle_selector' do
form.input selector, :as => :radio, :collection => [:yes, :no, '?']
end
end
def toggle_content
content_tag :div, :class => 'toggle_content' do
yield if block_given?
end
end
However, this will only work if i only have one input on the toggle_content div, because when i use the yield on the block of inputs, only the last one will be output.但是,这仅在我在 toggle_content div 上只有一个输入时才有效,因为当我在输入块上使用 yield 时,只有最后一个将是 output。
Anyone could give a hint on a better solution to refactor these to be able to accept more than one input on the toggle_content div?任何人都可以给出一个更好的解决方案的提示来重构这些以便能够在 toggle_content div 上接受多个输入?
Edited: Problem solved using the capture helper method instead of just yield.编辑:使用捕获辅助方法解决了问题,而不仅仅是屈服。 The code ended up like this on the helper:代码在帮助程序上最终是这样的:
def toggling_field_for form, selector, &block
content_tag :div, :class => 'toggle' do
%{#{toggle_selector form, selector}
#{toggle_content &block}
}.html_safe
end
end
private
def toggle_content &block
content_tag :div, capture(&block), :class => 'toggle_content'
end
And called like this on each form:并在每个表单上这样调用:
<%= toggling_field_for f, :was_answered, do %>
<%= f.input :name %>
<%= f.input :answer %>
<%= f.input :score %>
<% end %>
Thanks, mosch!谢谢,莫施!
With Rails 3, your code should work with a slight modification: Try changing使用 Rails 3,您的代码应该稍作修改即可工作:尝试更改
def toggle_content
content_tag :div, :class => 'toggle_content' do
yield if block_given?
end
end
to至
def toggle_content(&block)
content_tag :div, :class => 'toggle_content' do
capture(&block) if block_given?
end
end
I am not sure, but you can probably remove the block_given?我不确定,但您可能可以删除 block_given? clause in that case.在这种情况下的条款。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.