views:

90

answers:

1

I have this method to post the value to the "/store/add_to_cart"

<form action = "/store/add_to_cart" method="post">
<% for product in @products -%>
<div class   = "entry">
<%= product.title %>
<%= product.price %>
<p>
</div>
<% end %>
<%= select( "payment", "id", { "Visa" => "1", "Mastercard" => "2"}) %>
<%= submit_tag 'Make Order' %>
</form>

In the /store/add_to_cart.html.erb, I created :

<%= params.length %>
<% for i in params%>
<%=i%>
<br/>

<% end %>

But I get this error: ActionController::InvalidAuthenticityToken in StoreController#add_to_cart

What's happen? but after I change it to the get method, I can get all the params, wt's happen?

+8  A: 

You are not using rails form_for helper to generate the <form> HTML markup, what this method does in addition is to add a hidden input field that is used to prevent CSRF attacks.

You have three options:

  1. Use the form_for, form_tag... helper
  2. Include the hidden input yourself
  3. Disable the CSRF support
Zoran Regvart
How can I change my code to form_for helper way?
Ted Wong
Something like:<% form_for controller => 'store', :action => add_to_cart, :html => {:method => :post} do |f| %>...<% end %>but that's just not DRY enough, I would recommend that you do a more RESTful design, checkout: http://nubyonrails.com/articles/peepcode-rest-basics, http://ryandaigle.com/articles/2006/08/01/whats-new-in-edge-rails-simply-restful-support-and-how-to-use-it, http://www.b-simple.de/download/restful_rails_en.pdf (PDF)
Zoran Regvart