繁体   English   中英

如何在Micropost中包含可点击的网址?

[英]how to include a clickable url in Micropost?

从Michael Hartl的Rails教程中工作

我尝试了似乎无法正常工作的.html_safe

您可以在微博中发布任何url,但它仅显示为文本,没有链接。

    <li id="<%= feed_item.id %>">
  <%= image_tag feed_item.user.avatar.url(:small) %>
    <span class="user">
      <%= link_to feed_item.user.name, feed_item.user %>
    </span>
    <br><br>
    <span class="content">
    <%= feed_item.content.html_safe %></span>
    <span class="timestamp">
      Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
    </span>
    <div style="float: right; margin: 0px 0px 15px 15px;">
    <%= image_tag 'star.png' %><%= image_tag 'retweet.png' %>
    </div>
  <% if current_user?(feed_item.user) %>
    <%= link_to "delete", feed_item, method: :delete,
                                     data: { confirm: "You sure?" },
                                     title: feed_item.content %>
  <% end %>
</li>

您可以在这里https://github.com/tenderlove/rails_autolink使用此gem

安装gem后,只需像这样包装您的Micropost内容

<%= auto_link(feed_item.content) %>

正如@joey在下面的评论中提到的,对他来说,他必须将此添加到Gemfile中

gem 'rails_autolink', :require => 'rails_autolink'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM