ruby-on-rails - 如何将link_to放在rails中的字符串中

标签 ruby-on-rails ruby-on-rails-3

在这里我发现了类似的问题 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/

相关文章:

ruby-on-rails-3 - Bootstrap 选项卡内的 kaminari 分页

ruby-on-rails - Ruby 解析数组(特殊情况)

ruby-on-rails - 向我的大多数(但不是全部) View 添加 'user bar' 的最简洁方法

ruby-on-rails - ruby on rails 逻辑用于布局中的部分

jquery - Plupload、Carrierwave 和 Rails 3

ruby-on-rails - 创建一个简单的 Rails 3 文本助手 Gem

ruby-on-rails-3 - Rails 3 : displaying error full messages in flash message

windows-7 - 让 Rspec + 自动测试在 Windows 上运行

mysql - Ruby on Rails 服务器无法启动 : dyld: lazy symbol binding failed: Symbol not found: _mysql_get_client_info

ruby-on-rails - Ruby 在哈希集合中聚合选择性值