Creating a New Rails Application
To create a new Ruby on Rails application, open your command prompt and use the following command:
rails new blog
This command generates a new Rails project named “blog” with the necessary directory structure and configuration files.
Starting the Rails Server
To start the Rails server, use the following command:
rails server -b 0.0.0.0 -p 8000 -d
-b 0.0.0.0
binds the server to all available IP addresses, allowing it to be accessible from any network.-p 8000
sets the server to listen on port 8000.-d
runs the server in the background as a daemon.
Stopping the Rails Server
To stop the server, use the command:
kill `cat tmp/pids/server.pid`
This command reads the process ID (PID) from the file tmp/pids/server.pid
and terminates the server process.
Generating a Controller
To generate a new controller named “posts”, use the following command:
rails generate controller posts
This command creates the necessary files and folders for the “posts” controller.
Viewing Routes
To view the available routes in your Rails application, use:
rake routes
This command lists all the defined routes in the application, showing the URL patterns and the corresponding controller actions.
Generating a Model
To generate a new model named “category” with a string attribute “name”, use:
rails generate model category name:string
This command creates the necessary files for the “category” model and a migration to add the “name” attribute.
Running Migrations
To apply the database migrations, use:
rake db:migrate
This command updates the database schema based on the migrations defined in your application.
Opening Rails Console
To open the Rails console, use:
rails console
This interactive console allows you to interact with your Rails application’s models and data directly.
Generating Scaffolding
To generate scaffolding for the “category” model, use:
rails generate scaffolding category name:string
This command creates a full set of CRUD (Create, Read, Update, Delete) operations for the “category” model, including views and routes.
Installing Gems
To install the gems specified in your Gemfile, use:
bundle install
This command ensures all the required gems are installed for your application to run.
Installing Active Admin
To install the Active Admin gem, use:
rails generate active_admin:install
This command sets up Active Admin, a popular administration framework for Ruby on Rails applications.
Generating Active Admin Resource
To create an Active Admin resource for the “category” model, use:
rails generate active_admin:resource category
This command creates the necessary files to manage the “category” model within the Active Admin interface.