Create a Ruby on Rails app in less than a minute
Scaffold a Ruby on Rails application
5 min read
In my previous article, I provided brief info about Ruby on Rails. Now we are going to see how to create a new Ruby on Rails project within seconds with all basic tools in place.
P.S: Throughout our blog, we will refer to Ruby on Rails as just Rails.
- MVC: educative.io/blog/mvc-tutorial
- Ruby installed: ruby-lang.org/en/documentation/installation
- Then run
gem install railsto install Ruby on Rails
Creating new Rails project
To create a new Rails project, run
rails new <project_name. It is as simple as that.
rails new my-new-app
After running it, you will find your project created with the following directory structure:
The new application is created with
sqlite as default DB. It also assumes that you will use it as a full-stack framework, not just as an API. In order to customize what features you want to use, you can always pass the following options to it while creating it:
bin/rails server from inside the root of the new project to start the application. By default, it will run of port
3000. To specify a different port, we can pass it as an option, like
bin/rails server --port 3001. Open up your browser and navigate to
localhost:3000 to view your app.
Scaffolding an article app in less than a minute
A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
Let's now see how we can scaffold a full-fledged application in Rails. Stop the running server(
ctrl+c) and run the following command:
$ bin/rails generate scaffold Article title:string description:text invoke active_record create db/migrate/20210814102858_create_articles.rb create app/models/article.rb invoke test_unit create test/models/article_test.rb create test/fixtures/articles.yml invoke resource_route route resources :articles invoke scaffold_controller create app/controllers/articles_controller.rb invoke erb create app/views/articles create app/views/articles/index.html.erb create app/views/articles/edit.html.erb create app/views/articles/show.html.erb create app/views/articles/new.html.erb create app/views/articles/_form.html.erb invoke resource_route invoke test_unit create test/controllers/articles_controller_test.rb create test/system/articles_test.rb invoke helper create app/helpers/articles_helper.rb invoke test_unit invoke jbuilder create app/views/articles/index.json.jbuilder create app/views/articles/show.json.jbuilder create app/views/articles/_article.json.jbuilder invoke assets invoke scss create app/assets/stylesheets/articles.scss invoke scss identical app/assets/stylesheets/scaffolds.scss
Running the scaffold command crates the above files that include the model, migrations, controller, view, routes, etc. Now, we have run the migrations in order to create the
articles table with the
description fields that got created by the scaffold.
$ bin/rails db:migrate == 20210814102858 CreateArticles: migrating =================================== -- create_table(:articles) -> 0.0025s == 20210814102858 CreateArticles: migrated (0.0028s) ==========================
We can now, run the Rails server again
bin/rails server and visit
localhost:3000/articles to see the list of articles. Because we haven't created any, it will be shown empty but with a link to
New Article. Try playing with the application by creating few articles, view them on the list page, click on an individual article to get a detailed view.
It is a Rails-magic that without writing even a single line of code, we get a full-fledged app that supports all the CRUD operations, and everything works flawlessly. On opening the project in an editor, you will see all the MVC code that got created by the scaffold.
This blog is not intended to be a tutorial for beginners but a mere showcase of Rails scaffold. I will go into details of Rails in further blogs. Thanks for reading it till here.
Did you find this article valuable?
Support Akhil Gautam by becoming a sponsor. Any amount is appreciated!