GithubHelp home page GithubHelp logo

Comments (8)

xionon avatar xionon commented on September 1, 2024 2

@oguzbilgic for nested attributes, I have found it's almost always better to ignore your database model, and instead model your form as flat as possible.

As an example, assuming Person has_one Address, instead of doing this <input name="person[address][street_address]" />, just do <input name="person[street_address]" /> and figure it out on the server. For me, this has been true with and without React. I usually create an object that maps my REST-resource to the backend database models. That way, when the app grows and the backend modeling needs to change, it's much easier to change the way my REST-to-Database models work, rather than all the forms that interact with those models.

@eliotsykes sorry for missing your question. The form tag turned out to be very simple, so I never bothered making a PR for it. I posted a gist of my form helper. I never found a way to build Rails.FormErrorList that I really thought was re-usable, and the others are so trivial that adding them felt like it would just clutter the framework.

from react-rails.

atipugin avatar atipugin commented on September 1, 2024

I think it would be better to release it as a separate gem. Anyway, i totally up for it, as i had similar thoughts about Rails-specific components.

from react-rails.

eliotsykes avatar eliotsykes commented on September 1, 2024

@xionon I'd be interested in seeing any work-in-progress code for these helpers, they sound useful.

from react-rails.

oguzbilgic avatar oguzbilgic commented on September 1, 2024

I am also working on something similar. But the real issue is input naming, and nested attributes! I would love to hear if you guys found a solution to this.

from react-rails.

danott avatar danott commented on September 1, 2024

This is an awesome idea. 👍

from react-rails.

badenkov avatar badenkov commented on September 1, 2024

I also have similar ideas, i posted its here https://github.com/badenkov/react_form_rails

from react-rails.

danott avatar danott commented on September 1, 2024

This may be a terrible idea... but for the server side, what about an API that looks similar to the familiar form_for Rails helper?

<%= react_form_for @comment, 'CommentForm' %>

This would exist for the purpose of passing all the props that a <Rails.form /> component may need. It may generate something like:

<div data-react-class="CommentForm" data-react-props="{ RailsForm: { action: '/comments', csrfToken: 'foo', etc: '...' }, object: { comment: 'properties', would: 'be here' }" />

In my initial thinking, the object key would have @comment.to_json. And if that isn't what you want, a third optional parameter could be passed, to explicitly state object props.

Then, on the component side, we can use Rails components to build forms in a familiar way, adding behavior as we need it.

var CommentForm = React.createClass({
  propTypes: {
    RailsForm: React.PropTypes.shape({
      action: React.PropTypes.string.isRequired,
      csrfToken: React.PropTypes.string.isRequired,
      isNewRecord: React.PropTypes.bool.isRequied,
      errors: React.PropTypes.arrayOf(React.PropTypes.object),
    }),
    object: React.PropTypes.object.isRequred,
  }, 

  render() {
    <Rails.form {...this.props.RailsForm}>
      <Rails.label attr="text" />
      <Rails.inputField attr="text" onChange={this.handleTextChange" />
    </Rails>
  },

  handleTextChange() {
    /* validate? */
  }
})

from react-rails.

rmosolgo avatar rmosolgo commented on September 1, 2024

This would be a cool project, if anyone gets it going, please send a PR to the "Related Projects" section!

https://github.com/reactjs/react-rails#related-projects

from react-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.