在標題中我試圖創建這樣做的幫手,但我很掙扎。我得到錯誤或乾脆爲空列表如下: Ruby on Rails自定義幫助器輸出HTML嵌套列表
我要實現這一點: 有得多邏輯簡單地把這個代碼視圖。結果是散列,其中鍵是網站ID,值是書籤ID數組或書籤ID。
我的代碼:
module WebsitesHelper
def present_search_results(results)
content_tag(:ul, class: "websites-list") do
results.each do |key, value|
website = Website.find(key)
concat(content_tag(:li, website.url, class: "website-#{key}") do
bookmarks = website.bookmarks.select do |b|
if value.is_a?(Array)
value.include?(b.id)
else
value = b.id
end
end
content_tag(:ul, nil, id: "website-#{key}") do
bookmarks.each do |b|
content_tag(:li, b.title)
end
end
end)
end
end
end
end
你能提供一個你想要的嵌套列表的例子嗎? – Gerry
我剛剛添加了一張顯示我想要實現的圖片。我把所有的代碼放在視圖中,但我真的想使用助手。 – Gregg
因此,你想要顯示散列中的書籤或屬於每個網站(「模型」關聯)或兩者(即在散列中並屬於網站)的書籤? – Gerry