简体   繁体   English

如何在助手中连接link_to?

[英]How concatenate the link_to in helper?

How to make it work ? 如何使其工作?

I need puts the two links. 我需要puts两个链接。

Concatenation << with link_to does not. 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

You'll need to start with something before you can append to it with << , and then you'll need to call #html_safe to prevent Rails from escaping the HTML. 您需要先进行一些操作,然后才能使用<<追加到它之后,然后需要调用#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

The << operator is actually pushing an object onto an array. <<操作符实际上是将对象推入数组。 It also looks like the html variable is not defined yet. 看起来html变量尚未定义。 You create the array before your first link, then join it after your last, you should have what you need. 您在第一个链接之前创建数组,然后在最后一个链接之后加入数组,您应该拥有所需的内容。

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

Create a helper method in application_helper and using that you can create your link_to tag. 在application_helper中创建一个辅助方法,并使用该方法可以创建link_to标记。

Try this: 尝试这个:

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