Ruby-on-rails Rails / ERB 中的条件标签包装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5809917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:00:46  来源:igfitidea点击:

Conditional tag wrapping in Rails / ERB

ruby-on-railsrubyerb

提问by Joseph Ravenwolfe

What would be the most readable and/or concise way to write this in ERB? Writing my own method isn't preferable, as I would like to spread a cleaner solution for this to others in my company.

在 ERB 中编写它的最易读和/或最简洁的方法是什么?编写我自己的方法并不可取,因为我想为此向我公司的其他人传播一个更清洁的解决方案。

<% @items.each do |item| %>
  <% if item.isolated? %>
    <div class="isolated">
  <% end %>

    <%= item.name.pluralize %> <%# you can't win with indentation %>

  <% if item.isolated? %>
    </div>
  <% end %>
<% end %>

== Update ==

== 更新 ==

I used a more generic version of Gal's answer that is tag agnostic.

我使用了与标签无关的更通用版本的 Gal 答案。

def conditional_wrapper(condition=true, options={}, &block)
  options[:tag] ||= :div  
  if condition == true
    concat content_tag(options[:tag], capture(&block), options.delete_if{|k,v| k == :tag})
  else
    concat capture(&block)
  end
end

== Usage

== 用法

<% @items.each do |item| %>
  <% conditional_wrapper(item.isolated?, :class => "isolated") do %>
    <%= item.name.pluralize %>
  <% end %>
<% end %>

采纳答案by Gal

If you really want the DIV to be conditional, you could do something like this:

如果你真的希望 DIV 是有条件的,你可以这样做:

put this in application_helper.rb

把它放在 application_helper.rb

  def conditional_div(options={}, &block)
    if options.delete(:show_div)
      concat content_tag(:div, capture(&block), options)
    else
      concat capture(&block)
    end
  end

which then you can use like this in your view:

然后你可以在你的视图中使用这样的:

<% @items.each do |item| %>
  <% conditional_div(:show_div => item.isolated?, :class => 'isolated') do %>
    <%= item.name.pluralize %>
  <% end %>
<% end %>

回答by PreciousBodilyFluids

Try:

尝试:

<% @items.each do |item| %>
  <div class="<%= item.isolated? 'isolated' : '' %>">
    <%= item.name.pluralize %>
  </div>
<% end %>

回答by Unixmonkey

I like PreciousBodilyFluids' answer, but it doesn't strictly do exactly what your existing method does. If you really cannot have a wrapping div, this may be prefereable:

我喜欢 PreciousBodilyFluids 的回答,但它并没有严格按照您现有的方法所做的。如果你真的不能有一个包装 div,这可能是可取的:

<% @items.each do |item| %>
  <% if item.isolated? %>
    <div class="isolated">
      <%= item.name.pluralize %>
    </div>
  <% else %>
    <%= item.name.pluralize %>
  <% end %>
<% end %>

A helper method to do all of this would probably look like this:

完成所有这些操作的辅助方法可能如下所示:

def pluralized_name_for(item)
  if item.isolated?
    content_tag(:div, item.name.pluralize, :class => 'isolated')
  else
    item.name.pluralize
  end
end

Then your view code would look like this:

那么您的视图代码将如下所示:

<% @items.each do |item| %>
  <%= pluralized_name_for(item) %>
<% end %>