Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}" )
like image 358
Rahul Dess Avatar asked Jul 17 '14 21:07

Rahul Dess


Video Answer


1 Answers

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
like image 183
MrYoshiji Avatar answered Sep 30 '22 01:09

MrYoshiji