繁体   English   中英

如何在助手中连接link_to?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM