Rails: Having a small issue with link_to and external URLs - ruby-on-rails

Pretty new to rails. I'm doing a project where a user submits a url and a title. The title is supposed to link to the url provided by the user. The url is stored as a param for link.
Here's the code from the index view:
<% #links.each do |link| %>
<%= link_to link.title, link.url %>
<%= link_to "comments", link %>
<% end %>
This works, for the most part.
The problem occurs if the submitted url doesn't begin with http://. As it is, it's pointing to http://localhost:3000/google.com and I get an error No route matches [GET] "/google.com"
How could I get around this? I tried changing it to:
<%= link_to link.title, "http://#{link.url}" %>
Which makes google.com work, but then http://google.com turns into http://http//google.com somehow.
I'm sure the fix will be a face palm moment!

Prepend url with protocol if it's absent:
module ApplicationHelper
def url_with_protocol(url)
/^http/i.match(url) ? url : "http://#{url}"
end
end
<%= link_to link.title, url_with_protocol(link.url) %>
answer derived from this SO question/answer

In your input field, you can do something like <input type="text" name="url" value="http://"> so that your url will always started with http://. User can also manually changed it to https if needed.
Also I may add a full_url method to the model that adds it if it's missing.

Related

Rails: link_to a specific users show page

I'm trying to create a link that takes you to a specific users page in my rails app. The link currently looks like this:
<%= link_to track.user.username, user_path(#user) %>
However, I get this error when I try to refresh:
No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id]
Can somebody suggest what I could be missing? I'm not sure how to specifically reference the users id in the code.
Thanks,
Ant
Right apologies! I've figured it out, the link_to is placed inside of this slightly hidden loop in my code:
<% #tracks.each do |track| %>
// lots more code in here
<%= link_to track.user.username, user_path(#user) %>
<% end %>
so what i had to do was reference track first and then find the user like so:
track_path(track.user)
so my code now looks like so:
<% #tracks.each do |track| %>
<%= link_to track.user.username, user_path(track.user) %>
<% end %>
and this works perfectly fine!
Thanks all :)
Ant

Rails link from database appends to current controller

I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."

Link_to not working as expected

I am trying to build a simple app using Ruby on Rails. Essentially, I have a route that maps to a controller, whose view looks like this:
<div class="wishlist-container">
<% #wishlists.each do |w| %>
<div class="wishlist-card">
<h4><%= w.title %></h4>
<%= link_to "View List", wishlist_path(w) %>
</div>
<% end %>
Everything works correctly except for the link. For whatever reason, the link links to "." instead of "/" where <id> is the id. For example, it should link to /wishlist/1 but instead goes to /wishlist.1.
What is happening? How can I solve this problem?
For using paths helpers in your code , you should specify the resources , not only get or post in your routes.rb
for example if you've
get 'wishlist/:id' It may not work .To make your path work you should specify
get 'wishlist/:id', to: 'wishlist#show', as: 'wishlist'
For more information read Ruby docs and This article

Creating Links on Rails

I've just started using rails yesterday, so this is a kinda noob question
for example, a user is at www.example.com/name
and I want to make several links to www.example.com/name/:id
So I tried something like this:
<% #items.each do |item| %>
<%= link_to item.name, '/name' :id %>
<% end %>
I know, it was a complete guess on how I should write the code, but the restful code sends to a completely wrong link. How should I write this three lines?
Use the route helper:
<% #items.each do |item| %>
<%= link_to item.name, item_path(item) %>
<% end %>
ps: when you have a simple question like this one, take a look at this guide, you'll often find the answer.
Try
<%= link_to item.name, item_path(item) %>
item_path is a URL helper method which spits out the link to show a name.
URL helpers have the general form:
{action}_{class}_path({object or object_id})
If {action}_ is omitted, then the default action is assumed (normally show).

Linking to external URL object RoR

Currently when a user in my app creates a new job post they enter a URL that links to their job page on their website. I then want to use this to link out to their site.
It is stored in my DB asjob.job_url and I have defined an object #link in my controller to find the job and relevant URL.
I currently have the following code to loop through all jobs and provide links:
<section id="job-wrapper">
<% #jobs.each do |job| %>
<%= link_to #link.job_url do %>
<%= job.title %>
<%= job.location %>
<%= job.job_type %>
<%= job.salary %>
<%= job.company %>
<% end %>
<% end %>
</section>
The issue I am having is that without the URL containing http:// rails assumes that the link is internal and so produces a link like localhost:3000/www.google.com.
Is there a way for me to ensure Rails only ever recognises the link as external in the instance? Alternatively could someone explain how I can add http:// to all links on creation unless the user has already stated it?
Thanks in advance for your help!
Thanks for all your help!
I managed to solve this by creating a new helper called URLhelper
This contained:
module UrlHelper
def url_with_protocol(url)
/^http/.match(url) ? url : "http://#{url}"
end
end
I then just used the following link_to
<%= link_to url_with_protocol(#link.job_url), :target => '_blank' do %>
Thanks

Resources