I am using Rails 3 and javascript to make POST requests and therefore I need an authenticity token. However, the token Rails creates keeps including spaces, and I can't have that in my javascript URL string. Here's how it looks like in my log right now:
Started POST "/cart/update?authenticity_token=fjJJQc2gKBenzzAAqdvhprJxH2tnhYkyuZ9F+l+GFns=" for 127.0.0.1 at 2013-05-23 11:55:42 -0400
Processing by CartController#update as HTML
Parameters: {"gift_card_amount"=>"undefined", "case_quantity"=>"", "bottle_quantity"=>"", "product_history_id"=>"1052981", "authenticity_token"=>"fjJJQc2gKBenzzAAqdvhprJxH2tnhYkyuZ9F l GFns="}
WARNING: Can't verify CSRF token authenticity
This is my HTML:
<a href="javascript: void(0)" class="button-yellow">Add to Cart</a>
<%= hidden_field_tag form_authenticity_token %>
<script>
window._token = '<%= form_authenticity_token %>';
</script>
And my javascript:
$.post('/cart/update?authenticity_token=' + encodeURIComponent(window._token), ...
Notice I tried using encodeURIComponent
, but that doesn't seem to work. However, the problem still persists and I feel like it's Rails and not javascript. How would I change the Rails token to not include spaces? gsub for
+
like it does in javascript?