rails get entire current url but change the http protocol - ruby-on-rails

i need to create a link in my rails view that basically takes the user from the normal http page to the same page but using https.
i currently have something like
CLICK ME!
however it:
does not include the parameter string
just looks hack-ish to me
is there a better way to take the user from http to the same relative request path with all of the parameters?

Try something like
<%= link_to "CLICK ME!", named_url(protocol: 'https') %>
Where named_url is the route to whatever you want to link to.
EDIT
Since you don't have a named route and just want to use whatever page the user is on, you can do something like:
<%= link_to "CLICK ME!", "https://#{request.host}#{request.fullpath}" %>
You can find more about the request object here: http://guides.rubyonrails.org/action_controller_overview.html#the-request-object

Related

How to fix rails 4 hyperlinks?

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.

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

How to retrieve the URL of the current browsed Web page in my application and to add to it some query parameter?

I am using Ruby on Rails 3.2.2 and I would like to know how to retrieve the URL of the current browsed Web page in my application and to add to it some query parameter. That is, given a user is browsing the page http://www.my_application_name.org/articles/2, I would like to "build" something like link_to http://www.my_application_name.org/articles/2?param1=abc&param2=efg.
How can / should / could I make that?
You can use the url_for method and pass a ruby hash to add query parameters to current URL. For example,
<%= link_to 'Current URL with query params', url_for(param1: 'abc', param2: 'efg') %>
That should work.
If you want to get the current URL with host, port etc., you can use the request object to construct one as shown below.
<%= "http://#{request.host}:#{request.port.to_s+request.fullpath}" %>

Haml: link_to vs button_to

From what I understand, link_to is used for get methods, and button_to is used for post methods.
On the other hand, I was told that with HTML5 semantics, <button> is used for any type of clickable...well, button. In the case I have a clickable button that sends a user to a form to fill out, should I create a button_to or a link_to?
It's simpler that you think.
That methods are Rails helpers and don't have anything to do with haml.
Yes, one method is for get and another for post methods. If you need to post any data to controller, use button_to (for example when deleting a record). Otherwise, link_to is enough.
Moreover, you can make link_to posting data using :method parameter:
= link_to "Something", some_path, :method => :post
Answering your question, use link_to.
The main principle difference between the #link_to, and #button_to is that the #link_to just creates a link tag A, and makes simple AJAX request without an additional data, while #button_to creates a FORM with a custom data, so the form can be used to make extended AJAX request to a webserver. The form data includes embedded CSRF-token, which is used to authentication the request. In case of #link_to CSRF-token must be serualized and send in on_click event.
You should use links to point the user to a resource, like an article.
But you have to tend to use buttons to point to an action(like "Create"/"Send" on your edit page). If this doesn't agree with your interface -- style them like as a link.
Here's why: you cannot point your user to any non-GET action via link_to if he lacks the javascript support. So, buttons are the only options to make your send/destroy action to be triggered in this case.
Feel free to use both approaches if your link points to a page that eventually leads to a modification of a resource (link/button to an edit/create page that shows a form), like in your case.
If you want to simply send a user to somewhere, it is get request. So you should use link_to in this case. By the way, you can use the link_to for post requests and other requests (like button_to too) if you will specify :method. For example: =link_to "some path", some_path, :method => :get

the method problem and the difference between url and path

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.

Resources