Create a Demo project in Ruby on Rails
Step 1: Open the rails terminal, if you’re using Windows the terminal name will be ‘Ubuntu(WSL)’. Now run the following command. It will create a default project named ‘myapp‘ in the current directory.
rails new myapp
Step 2 : Use this command to get into your project directory.
cd myapp
Step 3 : Execute the below command in the terminal. It will create a controller file (home_controller.rb) in ‘app/controllers’ and corresponding view file (index.html.erb) which is our webpage in ‘app/views/home’. It will also create a route in ‘app/config/routes.rb’ which is needed for every webpage.
rails generate controller home index
Step 4 : Initially, when running the server, it gives the default welcome page as output. To display our home page, make changes in the ‘app/config/routes.rb’ file. Adding the following line in the file will set our home page as root.
root 'home#index'
Step 5 : Run the server using the command below and view the output at ‘http://localhost:3000/’. You can also view the default web page if you don’t make any change in ‘routes.rb’.
rails server
Output
Open the browser and paste the link ‘http://localhost:3000/’
How to create model in Ruby on Rails?
In Ruby on Rails, models are like the organizers of your data. They handle interactions with your database. Think of them as the brains of your application, responsible for managing and manipulating data. They represent the structure and behavior of the information your app works with. So, whenever you need to save, retrieve, update, or delete data, models are there to help you do it smoothly and efficiently. They make it easier to work with your data in your Rails application.
For example, you might have a ‘Book’ model that keeps track of details like the book’s title, author, and publication year. This model would have methods to retrieve a list of all books, find a specific book by its title, or update information about a book.
Before creating a model, let’s create a demo project in Ruby on Rails.
Table of Content
- Create a Demo project in Ruby on Rails
- Create model in Ruby on Rails
- Conclusions:
Contact Us