Neil
Neil

Reputation: 569

rails 4: URL Link

I have a web app which allows them to add a website url e.g www.google.com in a listings form so on the show page anyone can click on this and go the webpage www.google.com

Tried a few things link_to and etc but cannot seem to get it to work correctly

listings.form

<div class="form-group">
    <%= f.label "Tell Us About Your Business 25-30 chars max" %>
    <%= f.text_area :about, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label "Company Website" %>
    <%= f.text_field :url , class: "form-control", placeholder: "E.g www.google.com"  %>
  </div>

show.html.erb

 <div class="blog-header">
        <h1 class="blog-title"><%= @listing.job %></h1>
        <p class="lead blog-description"><b>Posted on</b> <%= @listing.created_at.strftime("%B %-d, %Y") %></p>
        <h4><%= @listing.company %></h4>
        <h4><%= @listing.location %></h4>
        ***<h4><%= link_to @listing.url %></h4>*** 
      </div>

so at the moment it creates a link but does not go to the webpage.

updated code

<div class="blog-header">
        <h1 class="blog-title"><%= @listing.job %></h1>
        <p class="lead blog-description"><b>Posted on</b> <%= @listing.created_at.strftime("%B %-d, %Y") %></p>
        <h4><%= @listing.company %></h4>
        <h4><%= @listing.location %></h4>
        <h4><%= link_to @listing.url, @listing.url %></h4> 
      </div>

Upvotes: 0

Views: 158

Answers (2)

j-dexx
j-dexx

Reputation: 10416

You need to pass two arguments to link_to

<%= link_to @listing.url, @listing.url %>

The first argument is the name of the link to display, if you don't set a second argument then rails will link to the current page

Upvotes: 7

James Mason
James Mason

Reputation: 4306

What you have should work fine, as long as you require tour users to type http:// or https:// before all of their URLs. Without that, the browser will interpret whatever you enter as a relative URL.

Upvotes: 0

Related Questions