Rails Generators

Randy Steele
4 min readOct 31, 2020

Today I am going to dive into a couple of the popular rails generators and what they actually do. First off we will take a look at the Scaffold Generator.

The Scaffold Generator — Let take a look and what the scaffold generator can do for us. I ran the following code below.

rails g scaffold Post content:string date:datetime reviews:integer

Once this code executes I get a whole bunch of new files that have been created. The scaffold generator created the migration file for us, the model file and since we didn’t use the — no-test-framework tag we also have test files for the model. The generator also added the resource of :posts to our config/routes.rb. We have a posts_controller file that was created for us and if we check that file out we will notice that we also have some pre-written code in that file, how convenient. We have an index, show, new, edit, create, update, destroy, set_post private method and post_params private method. Wow, that is a lot going on in the controller from the scaffold generator. This is convenient but it can also be annoying if you don’t need all of this stuff in your controller because you want to keep your code as DRY as possible, right? Let’s see what else the scaffold controller has done for us. We have a few views that have been created. We have views for index, edit, show, new, and a partial for _form. I think this partial is pretty nice. It has block of code to let you know of any errors that will prevent our posts from being saved. This could potentially save us some time coding this ourselves and is also very helpful for when you run into potential issues. This file also has a form precoded for us. There’s a few other files that were created as well but we are not going to focus on those at this point. My overall opinion on the scaffold generator is that is does A LOT for you but depending on the situation it may be a little too much and it could cause a lot of rework and in that case it might be better to just start from scratch or try a different generator. When I was learning ROR I was told to avoid this generator for the most part because of all the “extras” and the amount of time it could take to DRY it up. I’m going to use this on my next project though to see how much rework is actually needed and if it is time saving or not so much. What are your thoughts on the scaffold generator?

The next generator we will focus on here is the resource generator. This generator also does quite a bit for you but it’s not as overwhelming as the scaffold generator can be. So let’s take a look. I am going to run this snip of code in my terminal

rails g resource Author name:string email:string number_of_posts:integer

Let’s see what we have from the resource generator. It looks like the generator created the create_authors migration file, model, test file, controller, and a view folder for authors. Notice this generator does not create the actual view file for you it just creates a folder for those files to live in once you create them yourself. If you remember when we used the scaffold generator we had a total of 8 views created for us, some of those may not be necessary so this is nice and we don’t have to worry DRYing up our code here. If we take a look at the authors_controller you will notice there are no methods created as there were before with the scaffold generator so we will need to code these ourselves. In my opinion this is one of the best generators to use when your are building your application. It creates the necessary files for you and adds the route to the config/routes.rb file.

Let’s talk about some other generators that are available for use in Ruby on Rails. If you’d rather take it step by step instead of having the generator create files for you all at once you should think about using the rails g model (creates a model file for you) rails g controller (creates the controller file for you) If you want to add a migration file and no other files you can use the rails g migration generator. Note; With these migrations you will use the same syntax as the ones above. Tip: If you are creating attributes with the datatype of string you can leave off the “:string ” portion when doing so and rails will automatically create these as strings.

For more information on rails generators and to learn how to create your own generator take a look at the documentation here.

I hope this was helpful to anyone out there wondering the difference between these generators. Happy Coding!

--

--