没有路线匹配[GET]" /links/www.twitter.com.au"

时间:2015-02-20 13:46:11

标签: ruby-on-rails ruby

你好构建一个reddit克隆。

当我点击此处http://postimg.org/delete/saq41zozs/找到的链接时,它应该转到另一个网站。在这种情况下,它是www.twitter.com.au

此处发现错误http://postimg.org/delete/uhlsxso1e/

我猜是因为我没有twitter.com路线。我不知道如何设置它以便路由到twitter.com

这是我的routes.rb。

Rails.application.routes.draw do
  resources :comments

  devise_for :users

  resources :links do
    member do
      put "like",    to: "links#upvote"
      put "dislike", to: "links#downvote"
    end

  resources :comments
  end

  root "links#index"

这是我的show.html.erb

<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 :(得分:1)

当您链接到外部网站而不是相对网址时,您需要在链接前加上“http&#39;”。只需将相关链接更改为:

<div class="page-header">
  <h1><%= link_to @link.title, "http://#{@link.url}" %><br> <small>Submitted by <%= @link.user.name %></small></h1>
</div>