Rails中的content_tag与concat用法,可以连接任意html元素

 想输出如下html

<% if user.present? %>
    <li>

      <a href="<%= user_info_url(user.id) %>"><%= user.nickname %>
        <br><%= image_tag user.head_img_url %></a>

    </li>
<% else %>
    <li>

      <a href="#">空<br><%= image_tag 'blank.jpg' %></a>
      <p></p>
      <p><button class="btn btn-normal">点亮</button></p>
    </li>
<% end %>

  可以在helper中定义一个方法,内容如下:

content_tag :li do
      if user.present?
          html = link_to user_info_url(user.id) do
            concat user.nickname
            concat tag(:br)
            concat image_tag(user.head_img_url)
           
          end
         concat html
         concat content_tag :div ,'', class: 'mask'
      else
         link_to '#' do
           concat '空'
           concat tag(:br)
           concat image_tag('blank.jpg')
         end

      end

    end

  已经验证过!

posted @ 2016-07-27 18:11  PointNet  阅读(1190)  评论(0编辑  收藏  举报