I'm following these two tutorials altogether,
and implementing a simple comment system in Rails. However, I got a problem with handling the submit. I implemented a simple AJAX POST request to send my form data (as state). It looks like this:
var CommentForm = React.createClass({
handleChange: function(e) {
var name, obj;
name = e.target.name;
return this.setState((
obj = {},
obj["" + name] = e.target.value,
obj
));
},
handleSubmit: function(e) {
e.preventDefault();
var author = React.findDOMNode(this.refs.author).value.trim();
var text = React.findDOMNode(this.refs.text).value.trim();
if(!text || !author) {
return;
}
$.post(this.props.postUrl, {comment: this.state}, null, "application/json");
React.findDOMNode(this.refs.author).value = '';
React.findDOMNode(this.refs.text).value = '';
},
render: function() {
return (
<form className="commentForm" onSubmit={this.handleSubmit}>
<input type="text" placeholder="Your name" ref="author" name="author" onChange={this.handleChange} />
<input type="text" placeholder="Say something..." ref="text" name="text" onChange={this.handleChange} />
<input type="submit" value="Post" />
</form>
);
}
});
As I examined the console, It looks like it is passing to the right route and the right params, except no authenticity_token param. Here is a screen shot of what is in the console.
In the first link that I've provided, it is said that jquery_ujs handles generating and passing an authenticity token to Ajax POST. However, this is not true in my case. What am I missing?
Edit: I've got a few answers that would fix my problem. However, I'm still curios about what makes the difference between my code and the code at this tutorial?
