the method problem and the difference between url and path - ruby-on-rails

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.

Related

why's use edit_password_reset_url instead of edit_password_reset_path in password reset template

I saw this code in ROR application from railstutorial.org.
File: view/user_mailer/password_reset.html.erb
<h1><%= t ".heading" %></h1>
<p><%= t ".message"%></p>
<%= link_to t(".rs_password"), edit_password_reset_url(#user.reset_token,
email: #user.email) %>
<p><%= t ".timelink"%></p>
<p><%= t ".warning"%></p>
So, why use edit_password_reset_url. i use _path then rails have undefined method edit_password_reset_path. So, Have another way to use _path ?
I think a reason that, use _url return : http://localhost:3000/password_resets/{token}/edit?email=email08%40gmail.com
_path only retrurn: /password_resets/{token}/edit
it is because _path gives the relative path, and _url gives absolute path.
In an email, receiver needs an absolute path to your application
_path helpers provide a site-root-relative path. You should probably use this most of the time.
_url helpers provide an absolute path, including protocol and server name. I've found that I mainly use these in emails when creating links
to the app on the server. They should mainly be used when providing
links for external use. (Think email links, RSS, and things like the
copy and paste URL field under a YouTube video's "Share" section.)

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 get entire current url but change the http protocol

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

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}" %>

How to use http parametrs in https page in rails using rails-ssl_requirement plugin

I want to fetch the parameters of the form submit from the http page to the action which is required SSL. Is it possible?
actually i have a form on page http://www.mydomain.com/logins/new but when i submit the form it redirected to the https://www.mydomain.com/logins/create action but i don't get any parameters submitted from the new page form.
I solve this by requiring ssl for the new method and it works fine, but is it the only way to solve the problem or there is some other way to accomplish this so that i don't need to required on much of the actions.
If you're not using form helpers, just put the whole URL, including https, as the action for your form. If you are using form helpers, use the url (not path) and protocol option to specify https when you create your form. Like this:
<%= form_for #someobject, :url => the_create_url(:protocol => 'https') %>
You may have a slightly different set of options, but the main thing is specify :url with the url helper for your create action (ending in _url, not _path) and pass it the protocol option.

Resources