[英]How concatenate the link_to in helper?
如何使其工作?
我需要puts
兩個鏈接。
與link_to
串聯<<
不會。
module ItemHelper
def edit_links
if user_signed_in? && @item.user_id == current_user.id
html << link_to edit_item_path(@item), class: 'ui button small' do
"<i class='icon edit'></i> Edit"
end
html << link_to item_photos_path(@item), class: 'ui button small' do
"<i class='icon photo'></i> Photo"
end
html
end
end
end
您需要先進行一些操作,然后才能使用<<
追加到它之后,然后需要調用#html_safe
以防止Rails轉義HTML。
if user_signed_in? && @item.user_id == current_user.id
html = ""
html << link_to "<i class='icon edit'></i> Edit", edit_item_path(@item), class: 'ui button small'
html << link_to "<i class='icon photo'></i> Photo", item_photos_path(@item), class: 'ui button small'
html.html_safe
end
<<
操作符實際上是將對象推入數組。 看起來html
變量尚未定義。 您在第一個鏈接之前創建數組,然后在最后一個鏈接之后加入數組,您應該擁有所需的內容。
def edit_links
if user_signed_in? && @item.user_id == current_user.id
html = []
# ... existing logic
html.join
end
end
def show_link(link_text, link_source)
link_to link_source, { class: 'ui button small' } do
"#{content_tag :i, nil, class: 'iicon photo'} #{link_text}".html_safe
end
end
在application_helper中創建一個輔助方法,並使用該方法可以創建link_to標記。
嘗試這個:
def edit_links
if user_signed_in? && @item.user_id == current_user.id
link_1 = link_to edit_item_path(@item), class: 'ui button small' do
"<i class='icon edit'></i> Edit".html_safe
end
link_2 = link_to item_photos_path(@item), class: 'ui button small' do
"<i class='icon photo'></i> Photo".html_safe
end
link = link_1 + link_2
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.