I am working through Agile Web Development with Rails 4th Edition (Rails 3.2+) and I am somewhat confused by the button_to method. The book as well as every other google search I have done says the button_to uses a POST request. However, when I inspect the page, the button_to appears to be using a GET method.
Now, I could have this wrong, but I come from a php background and a POST method always meant that the data was being passed to the server without the url and accessed through an array server side:
$_POST['form_item']
Furthermore, a GET request meant passing data through the url:
someurl.com/somepage?DATA=data_value
Using this button_to code:
<%= button_to 'Add to Cart', line_items_path(product_id: product_id) %>
This becomes the form output:
<form action="/line_items?product_id=4" class="button_to" method="post">
<div>
<input type="submit" value="Add to Cart" />
<input name="authenticity_token" type="hidden" value="vGT1scGqasbQAMxYwofXHCM62MfSyM/pNty69Ep5t38=" />
</div>
</form>
the action on this form appears to be passing data through the url (?product_id=4). Although I can clearly see the “method=’post'”, isnt this a using GET?
As FancyDancy says, it’s still a post request even though the information gets passed through the URL. Rails doesn’t make a distinction between
$_GETand$_POST. It only has the equivalent of$_REQUEST:params. So it doesn’t really matter if the product_id gets passed via the URL or via a hidden form field. In both cases you can get it viaparams[:product_id].