Fist steps in Rails development

First, let’s display some text on the screen. This requires a running server for your Rails application.

Starting the web server

In fact, you already have a functional Rails application. To be sure, you need to run a web server on your machine. This can be done by running the following command from the blog directory:

$ bin/rails server

TIP: If you are using Windows, you must pass scripts from the bin folder directly to the Ruby interpreter, i.e. ruby ​​bin\rails server.

TIP: Compiling CoffeeScript and compressing JavaScript assets requires a JavaScript runtime environment on your system, and its absence will result in an execjs error during asset compilation. Typically, macOS and Windows come with a JavaScript runtime installed. Rails adds the mini_racer gem to the generated Gemfile of the new application in a commented line, you can uncomment it if needed. therubyrhino is the recommended runtime for JRuby users and is added to the Gemfile if the application is generated for JRuby. You can learn all about the supported runtimes in ExecJS

This will start Puma, the web server distributed with Rails by default. To see the application in action, open a browser window and navigate to http://localhost:3000. You should see the default Rails info page:

screenshot Welcome Aboard

TIP: To stop the web server, press Ctrl+C in the terminal where it is running. To verify that the server has been stopped, you should see the command line cursor again. For most UNIX-like systems, including macOS, this will be the dollar sign $. In development mode, Rails generally doesn’t require you to stop the server; all changes you make to files are automatically picked up by the server.

The “Welcome Aboard” page is like a test for a new Rails application: it shows that your programs are set up properly enough to display the page.

Say hello, Rails

For Rails to say “Hello”, you need to create at least a controller and a view.

The purpose of a controller is to receive specific requests to an application. Routing decides which controller will receive which requests. Often there is more than one route to each controller, and different routes can be handled by different actions. The purpose of each action is to collect information to provide it to the view.

The purpose of a view is to display this information in a human-readable format. An important difference to note is that the place where the information is collected is the controller, not the view. The view should only display this information. By default, view templates are written in a language called eRuby (Embedded Ruby), which is converted by the request loop in Rails before being sent to the user.

The most important of these are, of course, the controller located at app/controllers/welcome_controller.rb and the view located at app/views/welcome/index.html.erb.

Open the app/views/welcome/index.html.erb file in a text editor. Delete all existing code in the file and replace it with the following line of code:

<h1>Hello Rails!</h1>

Setting up the app’s home page

Now that we’ve made the controller and view, we need to tell Rails that we want to see “Hello Rails!” In our case, we want to see this when we visit our site’s root URL, http://localhost:3000. However, this place is now taken by the “Welcome Aboard” test page.

Now we need to tell Rails where the real home page is.

This is your application’s routing file, which contains site login options in a domain-specific language (DSL) that tells Rails how to connect incoming requests to controllers and actions. Edit this file by adding the root ‘welcome#index’ line of code. It should look like this:

Rails.application.routes.draw do
get ‘welcome/index’
root ‘welcome
#index’end

root ‘welcome#index’ tells Rails to route requests to the application root to the welcome controller’s index action, and get ‘welcome/index’ tells Rails to route requests to http://localhost:3000/welcome/index to the welcome controller’s index action. It was created earlier when running the controller generator (bin/rails generate controller Welcome index).

Restart the web server if you stopped it to generate the controller (bin/rails server) and navigate to localhost:3000 in a browser. You’ll see the Hello, Rails! label that you put in app/views/welcome/index, indicating that this new route actually leads to the index action in the WelcomeController, and the view renders correctly.

TIP. To learn more about routing, see the Routing in Rails guide.

We develop quickly

After you’ve seen how to create a controller, action, and view, let’s create something a little more tangible.

Now in the Blog application, we will create a new resource. A resource is a term for a collection of similar objects such as articles, people, or animals. You can create, read, update, and destroy elements for a resource, and these operations are called CRUD (create, read, update, destroy) operations.

Rails introduces the resources method, which is used to declare a standard REST resource. You need to add the article resource to config/routes.rb, this file will look like this:

Rails.application.routes.draw do
get ‘welcome/index’
resources:articles
 root ‘welcome#index’
end

If you run bin/rails routes, you can see that it has declared all routes for standard RESTful actions. We’ll look at the meaning of the prefix column (and the rest of the columns) later, but for now, note that Rails is aware of the singular form of the word article and uses this distinction judiciously.

In the next section, we will add the ability to create new articles and be able to view them. These are the letters “C” and “R” from CRUD: create and read. The form for this will look like this:

New article form

She looks a little simple now, but that’s okay. Later we will see how to improve its appearance.

Basics

First we need to create a new article somewhere. The most appropriate place would be /articles/new. With the route already defined, it is already possible to make requests to /articles/new of the application. Go to http://localhost:3000/articles/new and you will see a routing error:

Another routing error, uninitialized constant ArticlesController

This error occurred because the route needs a specific controller to serve the request. The solution to this problem is simple: create a controller named ArticlesController. This will be done by running the command:

$ bin/rails generate controller

If you open the newly generated app/controllers/articles_controller.rb, you can see a completely empty controller:

class ArticlesController < ApplicationController

end

A controller is simply a class that inherits from ApplicationController. In this class, you must define methods that will become actions for this controller. These actions will perform CRUD operations on the articles in your system.

NOTE: In Ruby, methods are public, private, and protected, but controller actions can only be public methods. See Programming Ruby for details.

Leave a Reply

Your email address will not be published.