link_to帮助程序没有到达外部链接

时间:2016-08-11 15:18:38

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

RoR新手,正在使用类似于pinterest的固定应用。

在我的form_for帮助器中,我尝试link_to外部网页

<%= link_to @pin.url, "http://#{@pin.url}" %>

网址保存为www.xxxx所以我添加了http,但我仍然收到错误:

No route matches [GET] "/www.xxx.com"

并且服务器正在尝试访问

localhost:3000/www.xxx.com

我似乎无法摆脱本地电话。 我已将我的部分表单视图附加到新的和编辑,以及 控制器文件。

并且作为旁注:由于这是在线课程的一部分,我无法添加额外的宝石作为前缀http或其他版本的http等的快速修复...

Partial _form:

<form class="form-horizontal" role="form">
  <div class="form-group">
  <% if !@errors.nil? %>
  <div class="error">
    <label class="col-lg-offset-3 col-lg-6"><%= pluralize(@errors.count, "error") %> prohibited this pin from being saved:</label>
    <ul>
      <% @errors.full_messages.each do |msg| %>
        <li><strong><%= "#{msg}" %></strong></li>
      <% end %>
    </ul>
    <% end %>
  </div>
</div>

<div class="form_group">
  <%= form_for @pin do |f| %>

    <div class="form-group">
    <label for="pin_title" class="col-lg-1">Title</label>
    <div class="col-lg-11">
      <%= f.text_field :title %>
    </div>
  </div>

  <div class="form-group">
    <label for="pin_category_id" class="col-lg-1">Category</label>
    <div class="col-lg-11">
      <%= f.collection_select(:category_id, Category.all, :id, :name) %>
    </div>
  </div>

  <div class="form-group">
    <label for="pin_url" class="col-lg-1">Web Address</label>
    <div class="col-lg-11">
    <%= link_to @pin.url, "http://#{@pin.url}" %>
    </div>
  </div>

  <div class="form-group">
    <label for="pin_slug" class="col-lg-1">Web short text</label>
    <div class="col-lg-11">
      <%= f.text_field :slug %>
    </div>
  </div>

  <div class="form-group">
    <label for="pin_text" class="col-lg-1">Description</label>
    <div class="col-lg-11">
      <%= f.text_field :text %>
      <%= f.text_area :body, size: "60x12" %>
    </div>
  </div>

  <div class="col-lg-offset-1">
    <%= f.submit "     PIN IT        " %>
  </div>
  <% end %>
</div>


</form>

控制器

    class PinsController < ApplicationController

  def index
    @pins = Pin.all
  end

  def show
    @pin = Pin.find(params[:id])
  end

  def show_by_name
    @pin = Pin.find_by_slug(params[:slug])
    render :show
  end

  def new
    @pin = Pin.new
  end



  def create
    @pin = Pin.create(pin_params)
    if @pin.valid?
        @pin.save
        redirect_to pin_path(@pin)
     else
        @errors = @pin.errors
        render :new
    end
  end

  def edit
    @pin = Pin.find(params[:id])
    render :edit
  end

  def update
    @pin = Pin.find(params[:id])

    if @pin.update_attributes(pin_params)
      redirect_to pin_path(@pin)
    else
      @errors = @pin.errors
      render :edit
    end
  end

  private

  def pin_params
    params.require(:pin).permit(:title, :url, :slug, :text, :category_id)
  end
end

Index.erb

 <div class="header">
  <h1>Learning to Code</h1>
  <h3>A Collection of Ruby on Rails Resources for Beginners</h3>
</div>
<hr/>
<div class="pins">
  <% @pins.each_slice(3) do |row| %>
    <div class="row">
      <% row.each do |pin| %>
        <div class="pin-container col-md-4">
          <div class="pin">
            <div class="pin-header row">
              <div class="logo-thumbnail col-xs-3">
                <% if pin.category.name == "rails" %>
                <%= image_tag('rails-logo-thumbnail.png')  %>
                <% elsif pin.category.name == "ruby" %>
                <%= image_tag('ruby-logo-thumbnail.png') %>
                <% end %>
             </div>
                <div class="col-xs-9">
                  <h4>
                    <%= link_to "#{pin.title}", pin_by_name_path(slug: pin.slug) %>
                </h4>
                <%= link_to "#{pin.url}", "#{pin.url}", html_options= {target: "_blank"} %>
              </div>
            </div>
            <hr/>
            <p class="text"><%= "#{pin.text}" %></p>
          </div>
        </div>
      <% end %>
    </div>
  <% end %>
</div>

<hr/>

0 个答案:

没有答案