简体   繁体   English

如何在Ruby On Rails中接受ReactJs的发布请求?

[英]How to take post request by ReactJs in Ruby On Rails?

As title, I tried to post my new form data to the rails server by using reactjs post request, but I got this in console: 作为标题,我试图通过使用reactjs发布请求将新的表单数据发布到Rails服务器,但是我在控制台中得到了这个:

Started POST "/comments.json" for ::1 at 2016-11-19 02:56:47 +0800
Processing by CommentsController#create as JSON
  Parameters: {"author"=>"v", "text"=>"c"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: comment):
  app/controllers/comments_controller.rb:73:in `comment_params'
  app/controllers/comments_controller.rb:28:in `create'

Controller: 控制器:

class CommentsController < ApplicationController
  skip_before_filter  :verify_authenticity_token
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  def index
    @comments = Comment.all
  end

  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end
  private
    def comment_params
      params.require(:comment).permit(:author, :text)
    end
end

React part 反应部分

class CommentForm extends React.Component{
constructor(props){
    super(props);
    this.state ={
        author: '',
        text: ''
    };
    this.handleValueChange = this.handleValueChange.bind(this);
    this.handleCreate = this.handleCreate.bind(this);

}
handleValueChange(event){
    valueName = event.target.name;
    this.setState({[valueName]: event.target.value});
};
handleCreate(event){
    event.preventDefault();
    $.ajax({
        method: 'POST',
        data: {
            author: this.state.author,
            text: this.state.text
        },
        url: '/comments.json'
    });
    console.log(this.state.author);
    console.log(this.state.text);
};
render(){
    console.log(this.state)
    return(
    <form onSubmit = {this.handleCreate.bind(this)} >
        <label>Author</label>
        <input type="text" placeholder="Author" value= {this.state.author} onChange={this.handleValueChange} name="author" />
        <label>Text</label>
        <input type="text" placeholder="Text" value= {this.state.text} onChange={this.handleValueChange} name="text" />
        <input type="submit" value="Create" />
    </form>
    );
}

} }

I don't know why post will get 400 request, and the param is missing. 我不知道为什么帖子会收到400个请求,而参数却丢失了。 How can I fix it? 我该如何解决?

You forget to add headers. 您忘记添加标题。 And rails controller waits your json schema with comment key in root. Rails控制器会在根目录中等待带有注释键的json模式。 Also url adjusted. 还要调整网址。

params.require(:comment).permit(:author, :text)

======= =======

$.ajax({
        method: 'POST',
        data: { comment:{
                author: this.state.author,
                text: this.state.text
               }
        },
        headers: {
         'Content-Type': 'application/json'
        },
        url: '/comments'
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM