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

Rah*_*ess 4 ruby-on-rails ruby-on-rails-3

在这里,我在一个字符串中发现了类似的问题Rails 3 link_to但不知何故它不适用于我的场景.

我有一个@matter像下面这样的url 的对象.

@matter = url_for(:controller => 'news', :action => 'news_view',:id => @news.token)
Run Code Online (Sandbox Code Playgroud)

我试图把这个链接放在文本中,这可以通过简单地添加"#{@ 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}" )
Run Code Online (Sandbox Code Playgroud)

MrY*_*iji 8

您可以执行以下操作:

"#{@news.sender.first_name} #{@news.sender.first_name} just sent you an invite."
"Click on link to #{link_to 'View invite', @matter}".html_safe
Run Code Online (Sandbox Code Playgroud)

如果在您的控制器中使用,您可以访问您的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
Run Code Online (Sandbox Code Playgroud)

  • 它说“#&lt;NewsController:0x007f4c4053b3d8&gt;的未定义方法‘link_to’” (2认同)