質問

I'm currently using the ruby-bitly gem to shorten links in a Rails app; however, the result has an "http://" in front of the bit.ly link. This is not friendly for tweets/other length sensitive posts. How do I automatically remove that?

Link controller action:

 def shorten_with_bitly(url)
    link = Link.find(params[:id]) 
    bitly = Bitly.shorten(url, "MY_ID", "MY_API_KEY")
    bitly.url = link.shortened_link
    link.save 
 end

Thank you very much for your help!!!

役に立ちましたか?

解決

Use gsub, for instance:

"http://google.com".gsub("http://", "")

or use slice:

"http://google.com".slice!(0..6) 

Keep in mind that the ! will modify the original string, not just give you a modified copy.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top