2013-11-03 12 views
0

我使用單個對象的partials像這樣:從hamls括號符號加前綴ID來防止同一ID的重複

#/app/views/people/_person.html.haml 
%div[person] 
    = image_tag person.image 
    %p.name= person.full_name 
    %p.location= person.location 

...所以我可以使用render方法,像這樣的對象組:

#/app/views/people/index.html.haml 
%ul.people 
    - @people.each do |person| 
    %li= render person 

不過,如果我渲染對象不止一次,我得到相同的ID,這是不那麼好兩個元素,如果只是用於驗證的緣故。有沒有辦法讓上面的單個對象保持局部,然後在調用render時,告訴它爲id加上前綴,以使id保持唯一?

舉個例子吧。說,我有一個臉書克隆的種類。在側邊欄中,我列出了一些您可能會感興趣的人,在主要區域我列出了所有我的朋友(我目前處於人員索引視圖中),並在標題中列出了最近嘗試過的人員列表與我聯繫。這三組可能共享一些相同的人,所以我會得到具有相同ID的多個元素。我不想給他們所有的號碼,因爲哈姆會用括號表示法,我想給邊欄中的人id interesting_person_654,標題中的人員號碼lately_calling_person_654和主要區域中的人員(該人員從當前的角度來看)編號爲person_654

我該怎麼做?

我想爲命名空間使用某些選項,但渲染方法只有4個選項,命名空間不是其中之一。儘管如此,我仍然可以用monkeypatching渲染方法。我寧願不使用ID前綴邏輯來遮蔽部分對象(我甚至不得不復制粘貼到其他所有對象部分 - 不太好!)。

+0

您是否考慮過使用'classes'。並使用'data'標籤來識別它們。 iE:「data-person-id = '12'」 –

回答

0

好吧,我發現了一個解決方案:

當通過在括號中的記錄,HAML使用記錄haml_object_ref方法來確定,使用什麼值類和ID前綴。因此,我改寫了渲染幫手,以便它可以呈現之前accordingsly調整這個methhod:

#header 
    %ul.lately_calling_people 
    %li = render @lately_calling_people[0], as: 'lately_calling' 
    %li = render @lately_calling_people[1], as: 'lately_calling' 
    %li = render @lately_calling_people[2], as: 'lately_calling' 

#sidebar 
    %ul.interesting_people 
    %li= render @interesting_people[0], as: 'interesting' 
    %li= render @interesting_people[1], as: 'interesting' 
    %li= render @interesting_people[2], as: 'interesting' 

#main 
    %div[@person] 
    @person.name 
    ... 

將產生下面的HTML:

# config/initializers/extensions/action_view_extension.rb 
# 
# (For some reason that is beyond be I could override this method in 
# ActionView::Helpers::RenderingHelper, so for not I patched it at class level) 
# 
class ActionView::Base 

    alias_method :original_render, :render 

    def render objects = [], options = {}, *args, &block 
    class_prefix = options[:class_prefix] || options[:namespace] || options[:as] 
    records = Array(objects).select { |object| object.is_a? ActiveRecord::Base } 
    records.each do |record| 
     class_name = record.class.to_s.underscore 
     record.define_singleton_method :haml_object_ref do 
     [class_prefix, class_name].compact.join('_') 
     end 
    end 

    original_render objects, options, *args, &block 

    end 
end 

現在我可以在這樣一個觀點叫render

... 

<ul class="lately_calling_people" 
    <li><div class="lately_calling_person" id="lately_calling_person_1">...</div></li> 
    ... 
</ul> 

... 

<ul class="interesting_people" 
    <li><div class="interesting_person" id="interesting_person_1">...</div></li> 
    ... 
</ul> 

... 

<div class="person" id="person_1"> 
    ... 
</div> 

,我還可以保持部分簡單:

# app/views/people/_person.html.haml 
%div[person] 
    = image_tag person.image 
    %p.name= person.full_name 
    %p.location= person.location 
0

the docs the Haml’s object reference syntax

另外,第二個參數(如果存在)將被用作ID和類屬性二者的前綴。

所以你可以改變:

%div[person] 

到(例如):

%div[person, :interesting] 

這將產生類似:

<div class='interesting_person' id='interesting_person_7'>... 

再後來:

%div[person, :lately_calling] 

將創建:

<div class='lately_calling_person' id='lately_calling_person_7'> 
+0

那麼,我知道,但它是如何解決我的問題?現在每個人都有ID'lately_calling_person_7'。 – Conkerchen