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
Related
I'm trying to display someone's profile links to their social media profiles. My current code is
<p>
<strong>Linkedin:</strong>
<%= link_to #person.linkedin, #person.linkedin %>
</p>
It works and the link does load, but it goes to localhost:3000/user/linkedin.com/in/user instead of linkedin.com/in/username
Thanks!
You need to add make sure the linkedin hyperlinks have a protocol (http, https, etc).
Any link without a protocol is assumed to be a relative path, which is why the hyperlinks are getting appended to your website url.
A solution would be to manually add a "http://" string at the beginning of every person's linkedin hyperlink in your database. Your code should work fine after that.
Edit: Or you can change it on the fly like so (the other answers won't work since it looks like #person.linkedin contains the entire hyperlink not just the linkedin user)
<%= link_to #person.linkedin, "https://#{#person.linkedin}" %>
Rails link helpers follow the format:
link_to(name = nil, options = nil, html_options = nil, &block)
The second #person.linkedin path is a local path as determined by your routes file in your config folder. If the link you need follows a certain format you can do something like
<%= link_to "LinkTextHere", "http://www.linkedin.com/#{#person}/profile" %>
I can answer in more detail if you give me the exact outcome you need as well as what you want from the .linkedin value. Also, typing "rake routes" in your console will show all paths you currently have and can help troubleshoot issues like why #person.linkedin is routing locally.
I have a form that let people enter various links on my website like: http, https and ftp. When someone enters "http:stackoverflow.com", "https:stackoverflow.com" and "ftp:stackoverflow.com" and renders these in a view like this:
<ul>
#foreach (var link in Model.Keyword.References)
{
<li>
#link.Link
</li>
}
</ul>
I get the following output:
http:stackoverflow.com
https:stackoverflow.com
ftp:stackoverflow.com
This looks fine but when I hover "http:stackoverflow.com" the real link is: "localhost:1973/stackoverflow.com".
When I hover the https and ftp links I get the correct output:
https://stackoverflow.com
ftp://stackoverflow.com
I found out that the http links requires two "forward slashes" when I submit them via a form. So instead of "http:" you get "http://". I would like to know why https and ftp links do not need these extra slashes. I would also like to know a good solution for this because I don't think I should force an user to use these extra slashes.
The slashes in the URL are always required. It could be that your browser does some correction to the invalid url. Try to put https:stackoverflow.com in the addressbar of your browser and it will try to correct it in most browsers.
As for a good solution, I think you should only accept the domain without http://. You can put some small text underneath the input that shows "Example: 'www.somedomain.com'", so that the user knows the protocol in the front is not needed.
You can then use JQUERY/REGEX to validate and trim off any protocol stuff someone might add mistakingly.
I have my my app url: 127.0.0.1:8080/reader/read.xhtml
The read.xhtml is populated thorough database and has got various
links which are hard coded in database. (read.xhtml is actually
retrieved as String from DB.) for e.g. there are links
(<a href ="/write.xhtml>write</a>)
/write.xhtml
/upload.xhtml
as I cannot add the context when i click the link it directs me to
WWW://127.0.0.1:8080/write.xhtml or
HTTP://127.0.0.1:8080/upload.xhtml
Is there any way I can redirect the link to
HTTP://127.0.0.1:8080/reader/write.xhtml.
Can Prettyfaces handle this. If yes how?
You can simply add the context path to the links by rendering it in front of your links. Something like:
write
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.
when i read the book "Aglie web development with rails 4th",i found the code
<%= button_to 'Add to Cart', line_items_path(:product_id => product) %>
what's the difference if i use "line_items_url" and the code doesn't has the method like :method=>:post,
why?
The path version produces relative urls such as /order/34/lines/ while the url version produces a full url such as http://localhost:3000/order/34/lines/.
The second form is often used in mailers when the user click a link in a mail client or in an external webmail.
In your site you won't notice any difference.
Moreover the :method=>:post option will produce a post request to your webserver. It will do that by adding a javascript code which will create a form on the fly, add parameters to it and do a submit call to send your browser to the requested page with a post method.
The _url helper generates an URL that includes the protocol and host
name. The _path helper generates only the path portion.