Rails - 无法访问链接

时间:2015-02-07 19:30:19

标签: ruby-on-rails ruby ruby-on-rails-4

我试图遵循Mackenziechild.me的教程。

以下是github https://github.com/ikanyu/raddit

中的代码

演示应用:https://whispering-shelf-9164.herokuapp.com/links/1

任何人都可以指出为什么当我检查我创建的链接上的元素时(例如Facebook链接),我可以清楚地看到facebook。但是,当我点击链接时,它会给我错误。

LinksController中的ActiveRecord :: RecordNotFound#show

无法找到与' id' = facebook

的链接
def set_link
 @link = Link.find(params[:id])
end

def authorized_user
 ...

谢谢!

<div class="page-header">
  <h1><a href="<%= @link.url %>"><%= @link.title %></a><br> <small>Submitted by <%= @link.user.name %></small></h1>
</div>

<div class="btn-group">
  <%= link_to 'Visit URL', @link.url, class: "btn btn-primary" %>
</div>

<div class="btn-group pull-right">
  <%= link_to like_link_path(@link), method: :put, class: "btn btn-default btn-sm" do %>
    <span class="glyphicon glyphicon-chevron-up"></span>
    Upvote
    <%= @link.get_upvotes.size %>
  <% end %>
  <%= link_to dislike_link_path(@link), method: :put, class: "btn btn-default btn-sm" do %>
    <span class="glyphicon glyphicon-chevron-down">
    Downvote
    <%= @link.get_downvotes.size %>
  <% end %>
</div>

<% if @link.user == current_user -%>
  <div class="btn-group">
    <%= link_to 'Edit', edit_link_path(@link), class: "btn btn-default" %>
    <%= link_to 'Destroy', @link, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-default" %>
  </div>
<% end %>

<h3 class="comments_title">
  <%= @link.comments.count %> Comments
</h3>

<div id="comments">
  <%= render :partial => @link.comments %>
</div>
<%= simple_form_for [@link, Comment.new]  do |f| %>
  <div class="field">
    <%= f.text_area :body, class: "form-control" %>
  </div>
  <br>
  <%= f.submit "Add Comment", class: "btn btn-primary" %>
<% end %>

1 个答案:

答案 0 :(得分:0)

如果您使用以下html在rails中创建链接

<a href="facebook.com">Facebook.com</a>

Rails会将您链接到YourApp.com/facebook.com(这是一个无效的网址)。

尝试重写这样的链接:

<a href="http://www.facebook.com">Facebook.com</a>

您需要将视图中的代码更改为:

<div class="page-header">
    <h1><a href="http://www.<%= @link.url %>"><%= @link.title %></a><br> <small>Submitted by <%= @link.user.name %></small></h1>
</div>

或者,您可以编辑@ link.url以包含字符串&#34; http://www。&#34;