Search code examples
ruby-on-railsrubytwitterhamlhref

Have an a href link to a variable twitter handle in ruby on rails


I'm trying to make a link that goes to a user's twitter account dynamically via a Twitter handle. The problem is the Twitter handle is just @variablehandle, not a full site link.

I figured out how to link to Facebook dynamically but Twitter was a fail, so maybe this can help you guys help me.

Both the Twitter and Facebook variables are present here. Also, we are using HAML:

- unless @user.facebook_url.blank?
  %a{:href => @user.facebook_url}
    click here


-unless @user.twitter_handle.blank?
  %a{:href => "https://twitter.com/" + @user.twitter_handle}
    click me

Solution

  • Use the link_to helper to make life easier:

    - unless @user.facebook_url.blank?
      =link_to "click here", @user.facebook_url
    
    - unless @user.twitter_handle.blank?
      =link_to "click me", "https://twitter.com/" + @user.twitter_handle