简体   繁体   中英

How concatenate the link_to in helper?

How to make it work ?

I need puts the two links.

Concatenation << with link_to does not.

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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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