Search code examples
ruby-on-railslink-to

custom link_to - ruby on rails


I have a class Thingy, whose objects often need to create links to other websites. So far the only approach working for me is the following:

class Thingy < Active:Record::Base
  def makeLink
    result = "<a href=\"someURL"+self.firstProperty+"\">"+self.secondProperty+"</a>"
  end
...

Now in any view I could use this link-method as follows

thingy.makeLink.html_safe

Somehow I feel there should be a much better way. What is the right approach here?


Solution

  • the proper approach would be to add a ThingyHelper in app/helpers

    def behavior_describing_method_name(thingy)
      link_to thingy.secondProperty, "someUrl#{thingy.firstProperty}"
    end