Rahul Dess
Rahul Dess

Reputation: 2587

How to put link_to inside string in rails

Here i have found similar question Rails 3 link_to inside a string but somehow its not working for my scenario.

I have a @matter object which has url like below.

@matter = url_for(:controller => 'news', :action => 'news_view',:id => @news.token)

I am trying to put this link inside text , which can be done by simply putting "#{@matter}". But i want to put a alias of that whole link generated like 'VIEW NEWS' and when user clicks on it, it will redirect to original url.

Using this i have to send to my link to both mail and mobile text. I got it for email but not able to do it for sending text, as :body requires string format for message API.

I have followed above mentioned SO post, but they are not at all working for me.

"\"#{@news.sender.first_name} #{@news.sender.last_name} just sent you a invite.\" Click on link raw('<%= link_to 'View Invite', @method) %>') ")

I am ok with using html inside string like <a href ="#{@matter}", view invite </a> if possible.

Help required.

I am using twilio to send text using below API.

  @client = Twilio::REST::Client.new @account_sid, @auth_token
  @client.account.messages.create(:from => '+123456789', :to => '+1'+@phone, 
                                  :body => "#{@matter}" )

Upvotes: 6

Views: 6168

Answers (2)

Rahul Dess
Rahul Dess

Reputation: 2587

It seems like we cannot include hyperlink in text message due to privacy issues. This post says so create hyperlink in sms in iPhone. That is why though i tried MrYoshiji's answers, i was not getting text properly as i wanted.

Upvotes: 0

MrYoshiji
MrYoshiji

Reputation: 54882

You can do as the following:

"#{@news.sender.first_name} #{@news.sender.first_name} just sent you an invite."
"Click on link to #{link_to 'View invite', @matter}".html_safe

If used in your controller, you can access to your view_context:

def show
  # ...
  @message  = "#{@news.sender.first_name} #{@news.sender.first_name} just sent you an invite."
  @message << "Click on link to #{view_context.link_to 'View invite', @matter}"

  @client = Twilio::REST::Client.new @account_sid, @auth_token
  @client.account.messages.create(:from => '+123456789', :to => '+1'+@phone, 
                              :body => @message.html_safe )
end

Upvotes: 12

Related Questions