在这里我发现了类似的问题 Rails 3 link_to inside a string 但不知何故它不适用于我的场景。
我有一个 @matter
对象,它的 url 如下所示。
@matter = url_for(:controller => 'news', :action => 'news_view',:id => @news.token)
我试图把这个链接放在 text 中,这可以通过简单地输入“#{@matter}”来完成。但我想把生成的整个链接的别名像“查看新闻”一样,当用户点击它时,它会重定向到原始网址。
使用这个我必须发送到我的邮件和移动文本链接。我收到了电子邮件,但无法发送文本,因为
:body
需要消息 API 的字符串格式。我遵循了上面提到的 SO 帖子,但它们根本不适合我。
"\"#{@news.sender.first_name} #{@news.sender.last_name} just sent you a invite.\" Click on link raw('<%= link_to 'View Invite', @method) %>') ")
如果可能的话,我可以在像
<a href ="#{@matter}", view invite </a>
这样的字符串中使用 html。需要帮助。
我正在使用 twilio 使用以下 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
如果在您的 Controller 中使用,您可以访问您的
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
关于ruby-on-rails - 如何将link_to放在rails中的字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24813895/