Rails中的反应形式与验证

时间:2016-01-27 15:20:17

标签: javascript ruby-on-rails validation reactjs coffeescript

我学习了ReactJS组件,验证时遇到问题 我有这段代码:

# postform.js.cjsx
 @PostForm = React.createClass
  getInitialState: ->
    link: ''
    errors: ''
  valid: ->
    @state.link

  handleChange: (e) ->
    name = e.target.name
    @setState "#{ name }": e.target.value

  handleSubmit: (e) ->
    e.preventDefault()
    $.ajax
      method: 'POST'
      url: "/posts/"
      dataType: 'JSON'
      data:
        post: {
            link: @state.link
        }
      success: (data) =>
        alert('success')
      error: (data) =>
        errors = data
        alert('error' + data)

  render: ->
    errors =
            <div id="error_explanation">
              <h2>Errors prohibited this product from being saved:</h2>
              <ul>aaa
              </ul>
            </div>
    React.DOM.form
      className: 'form-inline'
      onSubmit: @handleSubmit
      `[errors]`
      React.DOM.div
        className: 'well'
        React.DOM.div
          className: 'form-group'
          React.DOM.input
            type: 'text'
            className: 'form-control'
            placeholder: 'link'
            name: 'link'
            value: @state.link
            onChange: @handleChange
        React.DOM.button
          type: 'submit'
          className: 'btn btn-primary'
          disabled: !@valid()
          'Send link'

如何在视图中验证链接字段以添加bootstrap错误类并从Rails中提供有关验证问题的一些信息? 发布时有效负载,无效时:

{"link":["is invalid"]}

另一个问题 - 如何通过编写ReactJS表单或其他组件(例如rails中的simple_form)来提高速度?

0 个答案:

没有答案