External link routes in rails - ruby-on-rails

Simple enough. When were making a typical restful rails app we would keep all routes inside of our applicaiton. Very rarely would a path link to an external path. But if we were to do it, I'm wondering what the best way is.
A typical matching of home
match "home"=>"appcontroller#home"
If we were matching an external url to a variable of path. We might do something like the below?
First method
Routes.rb
match "external"=>"http:/www.google.ie"
Then in our html.erb
<%= link_to 'Google', external_path %>
Note this is not actually a legal way of doing things but something similar may exist. It seems very close to the current way of defining paths in rails but with an external landing.
Second method
Something that I've seen done elsewhere is to create a global variable for the external URL and use it in the link. EG.
in environment.rb or production.rb or whatever
#ext_path="http:/www.google.ie"
Then in our html.erb
<%= link_to 'Google', #ext_path %>
So to recap. Whats the best way to use external URLS in rails. Paths? Variables? Other?
Any input appreciated

I would have kept external links only in views. Because this links are not related to the any kind of logic of the application, and it's just an UI elements.
So, this way seems to me the best:
<%= link_to 'Google', "http://google.ie" %>
If you need to use this element many times, maybe it makes sense to bring this code into the helper, for example:
def search_engine_link
link_to 'Google', "http://google.ie"
end
And I really think that is's not very good place to introduce a more complex logic.

I'd probably use application-wide helpers wrapped around constants.
Helpers because I'd rather not see constants in templates, constants for environment-specific values.
I might use a hash to store the URLs: this keeps them more-tightly-coupled, and would allow environment-wide defaults, overriding per-environment as necessary.
The helpers could take symbols, or be generated from the hash keys, to generate xxx_path methods as happens with routes in routes.rb.

I found myself needing to do this because I had a link to an external site which I intended to make local in future.
In Rails 4 you can add this to config/routes.rb:
get '/contact',
to: redirect('http://www.example.com/contact.php'),
as: 'contact_us'
Which at the price of an extra redirect, lets you write:
<%= link_to "Contact us", contact_us_path %>

301-redirects
A note about use of redirect(...) in the routes file. Redirect generates a 301-redirect, which is a specific thing, Google describes it as...
If you need to change the URL of a page as it is shown in search
engine results, we recommend that you use a server-side 301 redirect.
This is the best way to ensure that users and search engines are
directed to the correct page. The 301 status code means that a page
has permanently moved to a new location.
So, using a 301-redirect in your routes for links to external websites may work, which it does, but the status information that is carried with that redirect is not correct.
If you've moved pages in your web app and want to tell the Google index (and everyone else) about it, then by all means use the redirect() to assure Google updates the index, but it's not optimal to use for standard external links out of your web app.

Related

Create Link to External Website

I am trying to create an external link to each individual listing's assigned website address. Using the following code: (The listing website is saved as google.com)
External Link
Takes me to:
localhost:3000/google.com
Is there any way to generate a link that would go to www.google.com instead of trying to find a route in my application.
The reason why it's bringing you to localhost:3000/google.com it's probably because the string you are passing to the href attribute is not a full qualified URL.
In fact, if in HTML you write
External Link
The string will be appended to the current page path. You should make sure that the input you pass always contains the schema. If the input never contains that, then you can assume it's http://
External Link
But this is not really a solution, just a workaround. You should definitely make sure that when you populate the website URL, you store a complete URL. In fact, some sites may require https.
In Rails you normally use the url_for and link_to helpers to generate an URL, but they will both cause the same issue unless you pass a full URL.
<%= link_to "External Link", "http://#{listing.website}" %>
Do it the Rails way:
<%= link_to 'External Link', "http://#{listing.website}" %>
You need to put in the protocol.
Google
Do you get it? =)
You can create link like this:
External Link

Rails 4: Any way to fix/use regular html href links w/o 'link_to'?

So my question is that I have a link to 'pages/home' and I click on it, ill go to my home page.
But then I'll try to click again, but the link changes to 'pages/pages/home' and then I'll get a routing error. Is there anyway to fix this using regular old anchor tags? or do i need to use link_to?
edit:
This is how i insert my link into the page.
Home
This is not related to rails, the problem is you use a relative url :
Home
This will lead to <any_path_you're_in>/pages/home.
For it to be absolute, you have to use (note the leading slash):
Home
By the way, it's quite a bad practice to use hardcoded url to your own rails app. You can avoid using #link_to while still taking advantage of rails' routing :
Home
Provided you have a "home" route, of course :
get '/pages/home' => 'pages#home', as: 'home'
This will save you a lot of pain when you decide to restructure your app.

Routing errors while processing URL(s) without a http prefix

We render HTML within a certain page and certain links don't have a http prefix (e.g. foo.com/bar) when you click on it throws a routing error. Is there a easier way to navigate to the right URL in such cases
Normally, the link_to takes care of you in this regard. You say html, so I assume we can't use ERB code, so you'll have to hardcode the links into the HTML.
Make sense?
Of course without any html code or routes.rb code, I can't tell you whether or not there isn't anything wrong with your existing code.
Why do you need to use absolute links within your application? Navigating to just "/bar" on that case would be fine.
If you use the link_to helper function on your views, it should rely on url_for which by default generates relative paths.
If you need absolute paths, you can specify a default host on your application (How do I set default host for url helpers in rails?) or specify it on the host option for the url_for function (http://apidock.com/rails/v3.2.13/ActionView/Helpers/UrlHelper/url_for).

Using MVC Routes as Shortcodes

We have been trying to implement shortcodes on an ASP.NET MVC web app that allow users to uniquely invoke a given article/page using an assigned short code.
For e.g.: www.mysite.com/power would map to an actual URL: www.mysite.com/Power/Home/.
I have created various routes throughout the site that map these shortcodes to various actions and controllers within the application. From a shortcode/route point of view, everything is working great.
I, however, noticed a couple of interesting things. I have hyperlinks that I use Url.Action method to generate the URL pointing pages. Many of these pages also have short codes associated with them. For e.g.: I have a link that says:
Go to Power page
This is a page that also has the previously mentioned short-code assigned to it. When I use Url.Action, I ideally expect it to create a link as /Power/Home/Index or /Power/Home, but since I also have a route constraint mapped to it, it now generates the link as /power.
Is there a way I can just use the actual link URL when generating links? I only want short-codes when I am sending out emails etc. I want the site to generate actual URLs.
This may or may not be possible, but I wanted to see if there were any ideas out there that I could use.
Anup
Index and Home are likely defined in your route table as defaults for the Action and Controller element. When you generate the Url it wont include the defaults if they aren't needed.
You could write your own Action overload or helper, which would allow you to take more direct control of the generated URL or action link. You could approach it from two different ways: 1) a helper to generate short-code specific urls and links, and/or 2) a helper to generate the full url and/or link. If Url.Action is returning the short-code version due to your routing configuration, I'd think a good place to start would be the second option, creating a helper/extension method that will generate the full url for you.
Here's how I solved this:
Instead of naming a route with short code to point to the action url, I made the route point to a different Controller action which would then redirect to the actual route that I want it to.
For e.g.: Originally I had the code "power" defined in the route table such that it would point to www.mysite.com/Power/Home.
Now instead of pointing it to that action - Index, controller - Home, area - Power, I make it resolve to: action - Power, Controller - Home, Area - ShortCode.
In the controller now, I simply do a RedirectToAction("Index", "Home", new { Area = "Power" });
This ensures that the actual links to /Power/Home do not resolve to the shortcode "power".
This is a simple fix increased the work by a little bit, but works like a charm.

Why does Rails use helper methods like link_to instead of <a href...>?

I am learning Rails and I have noticed that Rails continously uses helper method such as link_to instead of just using plain html. Now I can understand why they would use they would use some helper methods, but I don't understand why they would prefer helper methods over just coding the html directly. Why does Rails prefer helper method instead of you having to write the html manually? Why did the Rails team make this design choice?
In Rails apps, links are often generated using both methods for URL and methods for content, e.g.
<%= link_to #article.user.name, #article.user %>
That's definitely more manageable than putting those into the <a> tags manually.
<%= #article.user.name %>
(You are using the router for generating those URLs, right? What happens if you hardcode /users/1 and decide that you want it to be /users/1-John-Doe later?)
For static links, though, it doesn't really matter much.
Google
or
<%= link_to 'Google', 'http://www.google.com/' %>
One could make a case for the former for performance or the latter for consistent style, since both are equally manageable.
If the URL routing for a model changed, then hand-written HTML would all have to be updated as well; by going through a function the behavior (link to one of these things) is decoupled from the current routing scheme.

Resources