Y
Y
Yunique2018-08-29 22:25:14
Ruby on Rails
Yunique, 2018-08-29 22:25:14

How to write an HTTP request that would pass a variable from React to Ruby?

He asked a similar question , but he indicated incorrect data there, which confused me and the respondents.
Now the question boils down to this:
There is a form for React, you need to pass the value of one field (select) to the ruby ​​side.
Please tell me how to write the http request itself, and whether it is possible to write it directly in render () - where this value is displayed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-08-29
@Yunique33

This is how it is done without using redux:

const Example extends Component {
  state = {
    select: 'default value',
  };
  
  postData(data) {
    fetch('api/somePath', {
      method: 'post',
      headers: {
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    }).then(res => res.json())
      .then(data => {
        // do something with data
    });
  }  

  handleChange = e => { /* ... */ };  

  handleSubmit = () => {
    const { select } = this.state;

    this.postData({ select });
  };

  render() {
    return (
      <Form>
        <Select value={this.state.select} onChange={this.handleChange} />
        <Button onClick={this.handleSubmit}>Submit</Button>
      </Form>
    );
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question