【问题标题】:How to link_to from the content of the database in a html.erb page如何从 html.erb 页面中的数据库内容链接到
【发布时间】:2017-04-03 20:46:09
【问题描述】:

我正在使用 ruby​​ on rails 在 postgres db 上写一篇博文。表中帖子的内容有一些外部链接。我一直在使用 link_to 方法,但到目前为止,这只是在浏览器中从 html.erb 文件中打印了确切的代码。到目前为止,我已经尝试过:

<%= link_to 'London Coffee Festival', http://www.londoncoffestival.com %>

<% link_to 'London Coffee Festival', http://www.londoncoffestival.com %>

<%= link_to 'http://www.londoncoffestival.com'do %>London Coffee Festival<% end %>

但这些变化似乎都不起作用......

我的 html.erb 页面:

<%= render "partials/mainnav" %>
<div class="ui fluid image"><%= image_tag "header_img_thin.jpg" %>
</div>
<div class="ui grid container"><!--Main page container-->
  <div class="row">
    <div class="twelve wide column"><!--left Column-->
      <h1><%= @article.title %></h1>
<p><%= @article.text %></p>

<h2>Comments</h2>
<% @article.comments.each do |comment| %>
<p><strong>Name</strong>
<%= comment.commenter %>
</p>

<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>

<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %>
  <p>
<%= f.label :Name %><br>
<%= f.text_field :commenter %>
 </p>
<p>
<%= f.label :Comment %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>


</div><!--end left column-->
<div class="four wide column"><!--right column-->
  <p>
  Text
  </p>
  </div><!--end right column-->
  </div>
</div><!--End of main page container-->

【问题讨论】:

    标签: ruby-on-rails postgresql erb link-to


    【解决方案1】:

    文章中的链接应该是 html,而不是 ruby​​。所以应该是

    <a href='http://www.londoncoffestival.com'>London Coffee Festival</a>
    

    并将模板中的代码更改为

    <%== @article.text %>
    

    ==在这里很重要,它会告诉rails不要在文本中转义html

    【讨论】:

    • 很遗憾没有。这没有意义。
    • @SimonCooper 你确定你使用erb 吗?因为我只是复制粘贴了上面完全相同的代码,它可以工作
    • 是的 def,文件名是 app/views/articles/show.html.erb。我只是在本地主机上尝试这个。目前无法发布到生产环境。
    • @SimonCooper 好的,您能否提供show.html.erb 中的完整代码以及浏览器中渲染页面的屏幕截图?
    • @SimonCooper 感谢您的编辑。我已经更新了答案
    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 2011-04-23
    • 1970-01-01
    • 2013-12-17
    • 2021-11-21
    • 2020-06-26
    • 2011-09-28
    • 2023-03-15
    相关资源
    最近更新 更多