-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[proposal] Rails generator for components #72
Comments
Hi @xionon, I like the idea of having this kind of generators, specially this one for scaffold. Maybe we could have something like an unique scaffold generator for both rails and react. Assuming react is installed, maybe generate the react scaffold files when running the What do you think? |
I have been trying to think that through. Let's say you generate a scaffold for Post. <!-- app/views/posts/show.html.erb -->
<%= react_component 'Post', { post: @post }, { prerender: true } %>
<!-- app/views/posts/index.html.erb -->
<%= react_component 'PostTable', { posts: @posts }, { prerender: true } %> and Those are pretty easy. The scaffolded edit.html.erb and new.html.erb could be quite similar. <%= react_component 'PostForm', { post: @post }, { prerender: true } %> In this example, the form would need to reflect on the post object to determine if it was an edit or new object. Using only Rails' default JSON serializer, I think checking for an However, I don't think this quite hits the nail on the head. Sure, you get the e.g., when you click the "show" link from the posts index, you'll just get routed to Rails, which will render the component and send you a new page. You've lost the whole advantage of having a front-end JS library. I think that, if the react-rails gem is going to fully take over the default Rails scaffolds, it should also have an opinion on client-side routing. Maybe I'm wrong on that, though? Maybe it's totally fine to start with server-side rendering, and let the developers worry about catching clicks and rendering a different component. Philosophically, SHOULD react-rails provide a front-end routing solution? There are a couple out there, rackt/react-router and andreypopp/react-router-component both look like they should work on the server, but I'm not familiar enough with the JavaScript landscape to know if those are appropriate solutions or not. I could also see the argument that it should be done with a different gem - maybe |
Good question about the front-end routing. I would like to hear from others about this.
|
In #64, I laid the foundation for adding Rails generators. I'd like to discuss what other generators are possible, and what they might look like.
rails generate react:component [ComponentName]
I get really tired of typing /** @jsx React.DOM */ and all the other boilerplate that goes with creating a new component. Making a simple generator to scaffold up a new component is very straightforward.
rails generate react:component [ComponentName] [field:type] [field:type]
Similar to
rails g scaffold
, I would propose that this style automatically add an appropriatepropTypes
object as well as scaffolding out the JSX in a Rails-y way.Adapting one of the Rails scaffold examples, a react-rails component generator would look something like this:
rails generate react:component post title body:text published:boolean
which would generate something like this:
Note: I wrote this code without trying to run it, totally possible that I made some mistakes.
Wildly throwing spitballs:
Taking this even further, I wonder if it would be useful to create an entire scaffold generator? The addition of namespaced components makes this pretty intriguing to me. Running
rails generate react:scaffold_component post title body:text published:boolean
could create a set of components, something like Posts.Post, Posts.List, Posts.Edit, Posts.New, and Posts.Form, very similar to how Rails scaffolding currently works.Please comment and leave suggestions or requests! I'd love to build this next week, but I want to make sure it's both "React-y" and "Rails-y".
The text was updated successfully, but these errors were encountered: