如何将哈希对象传递给HAML标记

时间:2012-09-04 07:57:47

标签: ruby haml

请考虑这个例子:

- user_links_params = _user_link_params(current_user)

%a{ :'data-msgstore-path' => user_links_params[:'data-msgstore-path'],
  :'data-user_id' => user_links_params[:'data-user_id'],
  :class => user_links_params[:class],
}
  / too many html tags and stuff to fit in a simple link_to

我很乐意将这一切安排在如下的简单陈述中:

%a[_user_link_params(current_user)]
  / too many html tags and stuff to fit in a simple link_to

1 个答案:

答案 0 :(得分:10)

是的,这是可能的,你很亲密:

%a{_user_link_params(current_user)}

来自HAML reference

  

例如,如果您定义了

def hash1
  {:bread => 'white', :filling => 'peanut butter and jelly'}
end

def hash2
  {:bread => 'whole wheat'}
end
     

然后%sandwich{hash1, hash2, :delicious => true}/将编译为:

<sandwich bread='whole wheat' delicious='true' filling='peanut butter and jelly' />