如何将link_to放在rails中的字符串中 [英] How to put link_to inside string in rails

查看:60
本文介绍了如何将link_to放在rails中的字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我发现了类似的问题 Rails3 字符串内的 link_to 但不知何故它不适用于我的场景.

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

我有一个 @matter 对象,它的 url 如下所示.

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

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

我正在尝试将此链接放在 text 中,这可以通过简单地输入#{@matter}"来完成.但我想为整个链接添加一个别名,如查看新闻",当用户点击它时,它将重定向到原始网址.

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.

使用这个我必须发送到我的邮件和移动文本链接.我收到了电子邮件,但无法发送文本,因为 :body 需要消息 API 的字符串格式.

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.

我已经关注了上面提到的 SO 帖子,但它们根本不适合我.

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

"\"#{@news.sender.first_name} #{@news.sender.last_name} 刚刚给您发送了一个邀请.\" 点击链接 raw('<%= link_to 'View Invite', @method) %>') ")

我可以在 <a href ="#{@matter}" 之类的字符串中使用 html,如果可能,请查看邀请 </a>.

需要帮助.

我正在使用 twilio 使用以下 API 发送文本.

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}" )

推荐答案

您可以执行以下操作:

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

如果在您的控制器中使用,您可以访问您的view_context:

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

这篇关于如何将link_to放在rails中的字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆