Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 729
yerassyl Avatar asked Jul 05 '15 08:07

yerassyl


People also ask

How do you create forms in React?

In React, form data is usually handled by the components. When the data is handled by the components, all the data is stored in the component state. You can control changes by adding event handlers in the onChange attribute and that event handler will be used to update the state of the variable.

Can you use React with Rails?

There are two ways to use React with Ruby on Rails. The first is to build two separate, standalone apps, with the React app on a different repository and communicating with the backend via an API. This can simply be achieved by creating both apps separately using Create React App and the Rails CLI.

Is Ruby on Rails like React?

React and Ruby on Rails are two exceptional frameworks that developers use around the world. React enables developing interactive UIs and Rails is a full-stack technology. Using RoR for business logic & backend and React for frontend, your business can create a high-functioning application.


2 Answers

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.

like image 167
Pavel Kalashnikov Avatar answered Oct 11 '22 17:10

Pavel Kalashnikov


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.

like image 30
Blair Anderson Avatar answered Oct 11 '22 19:10

Blair Anderson