简体   繁体   English

ia 如何用 div 标签包围来自表单的多个输入?

[英]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.

相关问题 如何在simple_form中使用div来包围输入而不包围其各自的标签(使用f.input) - How can I surround inputs with a div in simple_form without surrounding their respective label (with f.input) Rails:以表格形式填充来自select标签的输入 - Rails: Populate inputs from a select tag in form 如何创建输入模型的多个项目的表单? - How do i create a form that inputs multiple items of a model? Rails-如何让表单将来自多个输入的数据保存为哈希 - Rails - How to have a form save the data from multiple inputs as a hash 如何避免在Rails 4中以表格形式重置输入 - How can I avoid reseting inputs in a form in Rails 4 如何在单击按钮时使表单中的多个输入显示为其他表单上的选择选项值? - How do I make multiple inputs in a form appear as a select option value on different form on button click? 如何将 rails 代码添加到 div 标签中的 id 属性? - How can I add rails code to id attribute in div tag? 当我<a>在轨道上</a>用<a>标签</a>包围时,为什么图像消失了<a>?</a> - Why does my image disappear when I surround it with an <a> tag on rails? 如何在rails上的ruby中将此form_tag表单转换为form_for表单? - How can I convert this form_tag form into a form_for form in ruby on rails? 我可以在表单内嵌套表单标签吗? - Can I nest form tag inside form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM