pipebender
pipebender

Reputation: 53

Setting rails routes to url path

I'm new to rails and having problems with my routes. When a link gets submitted, it should go to 'http://example.com' although it now goes to localhost:3000/links/www.example.com I am running ruby 3.2.8 and ruby1.9.3p194. Not sure how much info is needed. Here's where it's at. In my view I have:

<p>
<h1><%= @link.title %></h1>
<%= link_to @link.url, @link.url %><br />
 <p>Posted by: <% @link.user %> at <%= @link.created_at %></p>
</p>

My controller is set as:

class LinksController < ApplicationController
  def show
    @link = Link.find(params[:id])
  end

    before_filter :authenticate_user!

  def new   
    @link = Link.new
  end

  def create
     @link = Link.new(params[:link])

     respond_to do |format|
         if @link.save
          format.html { redirect_to @link, notice: 'Link was successfully  created.' }
          format.json { render :json => @link, status: :created, location: @link }
        else
          format.html { render :action => "new" }
          format.json { render :json => @link.errors, :status => :unprocessable_entity }
        end
     end
  end
end  

In my development.rb file I have:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

And my routes are:

resources :pages 
resources :links 
root :to => "pages#index"

After countless searches, I've had no luck since I am a beginner. Any suggestions for how to reset the link paths greatly appreciated.

Upvotes: 0

Views: 159

Answers (1)

user229044
user229044

Reputation: 239541

There is nothing to do with routes or Rails here.

You need to output http:// in front of your links, if you want to link off-site. Either enter in in the textbox when you submit your link, or modify your code to conditionally prepend it:

<% link_href = @link.url %>
<% link_href = "http://#{link_href}" unless link_href.match(/^https?:\/\//) %>
<%= link_to @link.url, link_href %><br />

Upvotes: 1

Related Questions