2012-09-04 27 views
8

Hãy xem xét ví dụ sau:Làm thế nào để vượt qua một đối tượng băm để một thẻ 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 

tôi sẽ được tốt đẹp để phù hợp với tất cả điều này trong một tuyên bố đơn giản như sau:

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

Trả lời

10

Vâng, nó có thể, và bạn đang ở rất gần:

%a{_user_link_params(current_user)} 

Từ HAML reference:

Ví dụ, nếu bạn đã định nghĩa

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

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

sau đó %sandwich{hash1, hash2, :delicious => true}/ sẽ biên dịch để:

<sandwich bread='whole wheat' delicious='true' filling='peanut butter and jelly' /> 
Các vấn đề liên quan