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

| 我目前正在开发一个Rails应用程序,该应用程序在许多形式和相同形式的许多领域中具有通用模式。这是模式:
<%= 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 %>
有一个主要的div标签围绕着另外两个div。根据来自toggle_selector div中输入的值,使用javascript使toggle_content div可见或不可见。 我试图提取几个助手,将输入作为参数并输出所需的输出:
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            
但是,只有在toggle_content div上只有一个输入时,这才起作用,因为当我在输入块上使用yield时,只会输出最后一个。 任何人都可以暗示一个更好的解决方案,以重构它们,使其能够在toggle_content div上接受多个输入? 编辑:使用捕获助手方法解决了问题,而不仅仅是屈服。代码最终在辅助程序上如下所示:
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
并在每种表单上这样调用:
    <%= toggling_field_for f, :was_answered, do %>
        <%= f.input :name %>
        <%= f.input :answer %>
        <%= f.input :score %>
    <% end %>
谢谢,莫斯!     
已邀请:
使用Rails 3,您的代码应该可以稍作修改:尝试更改
def toggle_content                                                       
  content_tag :div, :class => \'toggle_content\' do                        
    yield if block_given?                                                
  end                                                                    
end
def toggle_content(&block)
  content_tag :div, :class => \'toggle_content\' do                        
    capture(&block) if block_given?
  end                                                                    
end
我不确定,但您可能可以删除block_given吗?在这种情况下。     

要回复问题请先登录注册