Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

button_to using GET method

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?

like image 526
flyingarmadillo Avatar asked Nov 01 '25 01:11

flyingarmadillo


1 Answers

By default, it uses POST method. If you need GET, just type it as:

<%= button_to 'Add to Cart', line_items_path(product_id: product_id), :method => :get %>

You can check rails' logs and verify that it's really POST request.

like image 169
Alex Avatar answered Nov 03 '25 16:11

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!