Accepted answer

you can paste authenticity_token into react component.

using gem react-rails

= react_component 'form', authenticity_token: form_authenticity_token

form_authenticity_token is the rails helper.

so you can paste it into your form.

<form role='form' accept-charset="utf-8" action='/action' method='post'>
  <input type='hidden' name='authenticity_token' value={this.props.authenticity_token} />

it's not the best solution. i would be happy if someone will tell a better solution.


you can render your form inside an invisible block and then clone it into a react component

/* app/assets/stylesheets/users.scss */
.invisible-form-container {
  display: none;
# app/views/users/edit.html.haml
  = render 'form'
# app/assets/javascripts/components/
class @form extends react.component
  html_form: ->
    {__html: $('.invisible-form-container').html()}
  render: ->
    `<div dangerouslysetinnerhtml={this.html_form()} />`
# app/assets/javascripts/
$ ->
    `<form />`,

hope this helps.


if you render the base html with rails and embed a react component to it with react-rails, you can write like this:

var yourform = react.createclass({
  render: function() {
    var csrftoken = $('meta[name=csrf-token]').attr('content');
    return (
      <form action='/users' method='post' accept-charset='utf-8'>
        <input type='hidden' name='_method' value='patch' />
        <input type='hidden' name='utf8' value='✓' />
        <input type='hidden' name='authenticity_token' value={csrftoken} />


you can do something like this:

    headers: {
      'x-csrf-token': $('meta[name="csrf-token"]').attr('content')

and put the csrf-token into a meta tag inside your view/layout if it isn't already there.

Related Query

More Query from same tag