ruby-on-railsruby-on-rails-4view-helpershelpermethods

Create a list of clickable links in a Helper module


A user has a set of saved links.

Each link has the properties address and text. These can be accessed like so:

@user.links.first.address
@user.links.first.text

How would I generate a list of a tags for all links that a user has saved in a helper method, that I can call from a view?


Solution

  • you can try with

    def create_links
     html=""
     @user.links.each do |link|
      html += link_to "Link for #{link.address}", "#"
      html += link_to "Link for #{link.text}", "#"
     end
      html.html_safe
    end