在Rails的content_tag中包装if-else语句

时间:2010-10-29 06:22:20

标签: ruby-on-rails

如何在content_tag中包装if-else语句?

def entry_template
    content_tag(:div, :class => "item_title") do
      "<h3>


       if x == 'garage'
         #{entry.garage.name}
       else
         'Not garage'
       end


       </h3>"
    end
end

谢谢!

1 个答案:

答案 0 :(得分:1)

def entry_template
    content_tag(:div, :class => "item_title") do
      "<h3>" +
       if x == 'garage'
         entry.garage.name
       else
         'Not garage'
       end +
       "</h3>"
    end
end

或者,在我看来有点好,因为它不会一直切换样式,

def entry_template
    content_tag(:div, :class => "item_title") do
      content_tag(:h3) do
        if x == 'garage'
          entry.garage.name
        else
          'Not garage'
        end
      end
    end
end
相关问题