coletrain
coletrain

Reputation: 2849

Helper method for url?

I have multiple fields for social networking websites where users can enter their username and it would link to their twitter, facebook, myspace account etc. My problem is how can I have it so when a user enters their username I can link to their account.

Here's what I have and I know this is probably not correct. In my profile index.html

<%= link_to twitter_url %>

profile helper method

def twitter_url
  return(self.url= "http://twitter.com/#{profile.twitter}/account")
end

In my profile controller

include ProfilesHelper

The actual field in database is called

twitter_link

How can I get the just the user username then on my part which will do the rest link to their twitter, facebook etc account page?

Upvotes: 1

Views: 167

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107718

How is that method supposed to know what the profile object is without you passing it in?

I'm imagining there being a @profile variable defined in your controller. In the view, you would have this:

<%= link_to twitter_url %>

And in the helper, this:

def twitter_url
  "http://twitter.com/#{@profile.twitter_link}/account"
end

Note here that you don't need to set self.url or have an explicit return. This is because the method will automatically return a String which is what link_to takes.

If you want this method to be in a helper that's available in just your ProfileController, then why not put it inside ProfileHelper so that it's automatically included just by being related by name?

Upvotes: 4

Related Questions