[Rails] Pre-knowledge when coding for beginners (controller creation, action creation, non-action class creation, routing settings)

background

I summarized how to proceed with coding after building the Rails environment.

Also, if you are new to Ruby itself, you should read the following article. ・ Ruby Basic Grammar

Controller creation

The controller is basically created in model units (DB table units). Make the controller name plural. You can create the controller and the View file corresponding to the controller together with the following command. By the way, according to the Rails naming convention, the file name is snake case and the class name is camel case.

rails generate controller controller name

The controller name when executing the above command is Snake Case, and the file name created in either Camel Case is Snake Case. You can also create the controller manually without using the above command. In that case, let's also create a View file corresponding to the controller.

Action creation

An action is a method in the Controller. Create an empty method even if there is nothing to do in the action. If you do the following, you can create an action at the same time as creating a controller with a command.

rails generate controller controller name action name 1 action name 2

If the process you want to make a newly created action is already implemented in the existing action, you can reuse the existing action by using render. The following is an example.

def create
  #Returns a View corresponding to another action
  render action: :new
  #Returns the action of another controller
  render template: "hoge/new"
  #Returns json
  render json: @hoge
  #Returns text
  render text: "hoge"
  #Returns xml
  render xml: @hoge
end

The action manipulates the database through ActiveRecord and passes values ​​to View. Below are articles about the seven actions. [Ruby on rails] Action settings other than 7 actions

Creating classes other than actions

Create a directory anywhere in your project and create a class there. (App/commonclass/Hoge.rb, lib/Hoge.rb, etc.) After making it, load it from the controller as follows.

class HogeController < ApplicationController
  #Class loading
  require './app/commonclass/hoge'
  def index
  end
end

In addition to using require as described above, you can load it by adding the following to config/application.rb.

config.autoload_paths += %W(#{config.root}/The directory you want to read#{config.root}/The directory you want to read)

Routing settings

I referred to the following article. Rails Routing Basics Summary

Serpentine

Below is an article about naming conventions. -Rails Naming Convention

In particular, I referred to the following article. ・ First rails by experienced people in other languages -[Rails] I want to prepare a class file and call it from the controller

Finally, thank you to all the contributors of the article for your reference. I would appreciate it if you could point out any deficiencies in my article.

Recommended Posts

[Rails] Pre-knowledge when coding for beginners (controller creation, action creation, non-action class creation, routing settings)
[For beginners] Procedure for creating a controller using rails
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
[Rails] Complete routing settings