score:40
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.
score:1
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.
score:8
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>
);
}
});
score:13
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.
Source: stackoverflow.com
Related Query
- How to create forms with React and Rails?
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- How to determine the order of css in my create react app build with webpack and customize-cra?
- How can I get a full working example with React 0.14 and Typescript 1.6.2 and Rails 4.2
- How to Create a dropdown Button with see more option and on click toggle should not happen in react js?
- REACT: How to create a reusable custom hook with functions and effects that can change & react to changes in a host component's data
- How to dynamically create and render JSX elements, defined from this.props with react
- How to create sticky header with flexbox and REACT
- How to deploy React front end + Rails api backend with nginx, capistrano and passenger
- How to Create 3(country,state and city) dropdowns that populates data with each other in react js
- How to create a generic arrow function with react and typescript
- How to create a new page and route programatically in React.js with React Router
- How to create a dynamic, custom React Table Component with action and status buttons
- How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component?
- How to create a local react library project with common components, and import that project locally
- How to create multiple swiper instance in a react component and control one with the swipe of another?
- How to create a signup form with React and Why won't a function be executed when called insight a form after input field?
- I create a build of my react app and connected with electron.js. now how to connect my backend from server folder with electron
- How create React Typescript component with required and optional values but required value without default value?
- How to create a div with arrow and divider for content such that they are aligned using react and css?
- How to create an excel file in react js with custom headings and styles while doing export to excel?
- How to create bootstrap toggle buttons with React and Redux Form?
- How to create a form with text and file input in react and send form data to server using Nodejs?
- How to create a typeahead menu in React and populate it with options from an Postgresql table?
- How to create an input formfield with button that uploads a file using react and javascript?
- How to map through array of objects and create a new array of objects with a new added property using react and javascript?
- How to create a ref to child component, store it in context and pass it to a sibling in React with Material UI
- How to create floating image transition with css and react
- How do I create image maps (clickable areas on an image using <map >) with React and JSX?
- How to create module with react components and load with react lazy?
More Query from same tag
- When working with lists and keys in React, can Keys contain whitespace/spaces?
- React: hook-using component won't recognize current state of parent
- Use JavaScript variable from file in React
- Double Click on Kendo Grid row's for React Component
- React native async/wait not waiting
- Can't get :hover or any pseudo-classes to work in react + material UI
- React Router Switch statement with routes grouped as component inside does not go to Not Found route
- Creating dynamic table with data fetched from API in JS React Bootstrap
- Simplest way to dispatch and receive a custom event with React Hooks
- How Can I create a select input tag using Buttons and store the selected one in state
- Loading .svg files in img tag with nextjs9
- React doesn't change render according props invoked after a conditional rendering
- globalThis not defined after git hiccup
- how to show label between two nodes in react?
- Unexpected end of MIME multipart stream. MIME multipart message is not complete. Web API and superagent
- React Hooks Error Handling Turns State To Function
- How to describe the method in a React component with interface of TypeScript?
- How to avoid the Reactjs (Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.). from below component
- Why am I getting Failed prop type using react router and nested components
- Syntax Error when test component with SASS file imported
- moment.js convert to ISO output null + warning on specific date
- How can I pass props to children of React Router?
- using user input to setstate and then using the value of the state as part of the url for an axios request
- return is not working in async await function in react js
- Update component state when props change
- How to parse useState to determine singular active class for mapped compontents?
- sizing reactjs images to window size
- React hook state not being updated
- setState in didComponentMount sets state property to undefined
- React map doesn't re-render as state changes