I just made my debut in Ruby on Rails at work the other day. When it comes to Rails, I had the impression that a Web service with screen display was created, but at work, the API was created with Rails. So, here I will try both creating a web service and an API.
Windows 10 Home 64-bit
Rails works with a mechanism based on the MVC model. MVC comes from the acronyms "Model", "View", and "Controller", respectively, and often appears when developing applications with Rails.
Create a web screen that displays the "Hello World" that is customary for beginners.
Execute the following command to create a new project for the web service.
Command line
rails new application name
The folder structure under the application folder is in Rails Documents. You can check it in "What is Ruby on Rails> Folder structure> Folder list".
Run the following command to create the controller. When creating a controller, move to the application folder created earlier and then create it.
Command line
cd application folder
rails generate controller controller name
* Here, the controller name is "hello_world".
The controller "hello_world_controller.rb" has been created under the "app / controllers" folder. The source code at the time of generation is as follows.
hello_world_controller.rb
class HelloWorldController < ApplicationController
end
The methods defined in the controller are called actions. Edit the "hello_world_controller.rb" created earlier and try to define the "index" action.
hello_world_controller.rb
class HelloWorldController < ApplicationController
def index
end
end
The controller and action were created separately here, but it is also possible to create them all at once when creating the controller. To create all at once, execute the following command.
Command line
rails generate controller controller name action name 1 action name 2...
#Example
rails generate controller hoge index create
Under the "app / views / hello_world" folder, add the view "index.html.erb" corresponding to the index action.
Since it is assumed that "Hello World" will be displayed this time, describe as follows in "index.html.erb".
html:index.html.erb
<h1>Hello World</h1>
Receive the request from the browser and add the setting to distribute to the controller. The file to be modified is "routes.rb" under the "config" folder.
routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
get '/helloworld', to: 'hello_world#index'
end
You can check the format in "Routing Definition" in Rails Document. This time, I described the get method corresponding to the GET request.
Run the following command to start the Rails server.
Command line
rails s
Go to http: // localhost: 3000 / helloworld.
Create an API that returns "Hello World" in JSON format. The general flow is the same as for Web services.
Execute the following command to create a new API project.
Add --api
to a normal command and create a project in API mode.
Command line
rails new application name--api
The execution command is the same as the Web service. This time, let's create a controller and an action at once. The controller name and action name are the same as for the Web service. You can see the effect of API mode when creating a project here. If you create a controller normally, files related to views such as view, helper, and asset will be created at the same time, but those files will not be created because the API does not require views.
Add a process to return "Hello World" in JSON format.
hello_world_controller.rb
class HelloWorldController < ApplicationController
def index
render json: { data: "Hello World" }
end
end
Since the controller and action were created at once, the routing settings are automatically added, but I will change them.
Change before_routes.rb
Rails.application.routes.draw do
get 'hello_world/index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
After change_routes.rb
Rails.application.routes.draw do
get '/helloworld', to: 'hello_world#index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
Run the following command to start the Rails server.
Command line
rails s -p 3001
Execute the following command and confirm that JSON is returned.
Command line
curl --request GET --url http://localhost:3001/helloworld
"Hello World" is back in JSON safely!
Recommended Posts