用于创建网格的Ruby On Rails助手

时间:2014-02-25 13:24:46

标签: ruby-on-rails ruby-on-rails-4 haml

我正在尝试为网格创建一个帮助器,我正在使用此代码:

app / helpers / application_helper.rb中的

module ApplicationHelper
  def grid_helper(names)
    names.each_slice(2) do |name1, name2|

      haml_tag 'div', class: 'column' do
        haml_tag 'div', class: 'thumb' do
          haml_concat link_to name1[:url] do
            image_tag name1[:thumb], alt: name1[:description], size: '36x36'
          end
        end

        haml_tag 'div', class: 'thumb' do
          haml_concat link_to name2[:url] do
            image_tag name2[:thumb], alt: name2[:description], size: '36x36'
          end
        end
      end
    end
  end
end

呼吁haml:

#mydiv
  - games = [{thumb: 'foo', description: 'foobar', url: 'foo'}, {thumb: 'bar', description: 'foobarbaz', url: 'bar'}]
  - grid_helper(games)

所以这个用html:

<div class="column">
  <div class="thumb">
    <a href="/">foo</a>
  </div>

  <div class="thumb">
    <a href="/">bar</a>
  </div>
</div>

link_to无法正常工作且image_tag对我的帮助无效,有人可以提供帮助吗?

Obs:我正在使用Rails4,Ruby2和sass / haml

1 个答案:

答案 0 :(得分:1)

问题出在以下几行:

haml_concat link_to name1[:url] do ...

该块由haml_concat代替link_to使用。你可以像那样重写它

haml_concat(link_to name1[:url] { image_tag name1[:thumb], alt: name1[:description], size: '36x36' })