Be aware of getting out of Rails beginners (* will be added at any time)

I want to end the time when the code should work I think, look it up and make a note

Also write what you are doing in business and pointed out

Ruby edition

Method

** Instead of writing multiple logic in one method ** abstract into another method Create a method for each logic or operation and call it

Example) When the operation is branched according to the condition in the if statement Instead of writing more than one operation directly in that ** method ** Create a method for each operation and call it

constant

Specific numbers used in logic are ** stored in constants **

When making logic, if you write raw numbers, it will not be transmitted to first-timers ** Store in a named constant ** to see what that number means **

Example) Tax ** TAX = 10 ** Basic storage ** DEFAULT_STORAGE = 5 ** etc.

Rails

Routing Not only the order in which the routes are written, but ** where you can nest ::

Routing that sets its own URL It is easier to understand if you write it nested in resources

config/routes.rb


Rails.application.routes.draw do
  resources :posts do
    resource :likes
  end
end

Controller By adding the only and expect options to the routing resources Visualize which resources you are using

Inherit class and use super method

public methods are for actions only

The destination of processing is basic ** Model ** If it seems difficult to move to Model and refactor at the same time, After projecting all the contents of the action on the Model It is also good to take the procedure of returning to the Controller if necessary.

Model Write business logic in model Putting in concerns/is just ** processing common to multiple models **, ** Processing across classes ** is now concern

** Module ** in the same file Concern the group of processes you want to use in multiple classes and mix-in to the class to be imported.

** Write the process that spans multiple models ** in the ** service layer **

Use ** scope ** as a way to retrieve data from model In that case, you can use it like a method ** if you define it in ** scope, without having to write the condition every time by using the where clause a lot.

View Making common parts (** partial template, partial ) Do not write logic in view ( helper, helper **)

Define in ** Helper method ** Do not write logic in view as much as possible, and describe methods related to appearance in the helper

Separate into ** js files ** for each page Do not put script tags in erb file

Do not write style directly in view

Classes dedicated to js are distinguished by adding a js-prefix

Other

Define a string to be used repeatedly in ** locale ** Locale is used for time format, etc., and strftime is not basically used. You can also specify the amount, distance, date, etc.

Writing as short as possible, refactoring → Postfix if, ternary operator, bocchi operator, nil guard,% notation, etc.

Reference article

https://qiita.com/TOSHIMITSU_MIYACHI/items/fec069bcdf23b6be7623 https://qiita.com/d0ne1s/items/fabbc0df7c273cf04fc3 https://qiita.com/shunsuke227ono/items/60de21690238aa25e9d4

Recommended Posts

Be aware of getting out of Rails beginners (* will be added at any time)
[Rails] When using ajax, be aware of "CSRF measures".
Power skills that can be used quickly at any time --Reflection