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

发布于 2024-11-09 02:54:14 字数 2863 浏览 3 评论 0原文

我目前正在开发一个 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。使用javascript,根据toggle_selector div 中输入的值,使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 %>

谢谢,莫施!

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. 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. The toggle_content div is made visible or not, using javascript, based on the value from the input that is in toggle_selector div.

I tried to extract a couple helpers that would take the inputs as parametes and output the desired 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.

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?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-11-16 02:54:14

使用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?在这种情况下的条款。

With Rails 3, your code should work with a slight modification: Try changing

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? clause in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文