yerassyl
yerassyl

Reputation: 3048

How to create forms with React and Rails?

With Rails I create forms using the form_for tag.

Rails generates the HTML form automatically, including the authenticity_token for security against Cross-Site Request Forgery (CSRF).

With React I use JSX, so I cannot render erb in a React component.

In a React component I write HTML manually.

I want to use React in my Rails app, and still have the advantages of Rails.

Or, if there is no way to get the Rails advantages of form_for when using React as V of my app, how can I write a proper form as a React component?

If I first write my form_for and then look at the rendered HTML, copy it, and paste into my React component, is that a good idea?

For example here is rendered HTML from form_for:

<form class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="1AXH9blzaH4qEAAWTvu6aAH84btA/9DZCpFDBhiJ0/H9uAKsPAB/rFQWY1VmWA1yNtFaigO50p6joa3X8CItBA==" />
  

  <div class="field">
    <label for="user_Имя">Имя</label><br>
    <input placeholder="Бил Гейтс" type="text" name="user[name]" id="user_name" />
  </div>


  <div class="actions">
    <input type="submit" name="commit" value="Открыть заявку" class="button tiny" />
  </div>

</form>

Upvotes: 36

Views: 15002

Answers (4)

kraft
kraft

Reputation: 11

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
.invisible-form-container
  = render 'form'
.react-container
# app/assets/javascripts/components/form.js.jsx.coffee
class @Form extends React.Component
  html_form: ->
    {__html: $('.invisible-form-container').html()}
  render: ->
    `<div dangerouslySetInnerHTML={this.html_form()} />`
# app/assets/javascripts/initializer.coffee
$ ->
  ReactDOM.render(
    `<Form />`,
    $('.react-container')[0]
  )

Hope this helps.

Upvotes: 1

Tsutomu
Tsutomu

Reputation: 5148

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} />
        ...
      </form>
    );
  }
});

Upvotes: 8

Pavel Kalashnikov
Pavel Kalashnikov

Reputation: 2091

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} />
  ...
</form>

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

Upvotes: 40

Blair Anderson
Blair Anderson

Reputation: 20171

You can do something like this:

$(document).ready(function(){
  $.ajaxSetup({
    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.

Upvotes: 13

Related Questions