Do not write 〇〇 logic in Rails View (* added later)

I'm using Rails and I have methods for view, controller and model Because it became a matter of how to use it properly and define it Describe as a memorandum

The role of View

Page display, page transitions, and user-entered information Has a role such as sending to the controller ** Presentation logic **

〇〇 Do not write logic

The word that goes into 〇〇 is ** business ** logic Image of data and data-related processing, processing to save and read in DB

Why not

・ If the same business logic is duplicated in View, Wider range of changes and more costly → If you write in Model, there is only one place to change

・ Because it is necessary to write a test via View It is costly to test all for the same business logic → If you write this in `Model, you only need one test

・ If the design changes frequently The change can have an impact on your business logic

Be conscious

View just displays! simple When writing logic, use Controller, Model, Helper, Decorator, etc. Use properly

In addition, I will add it later!

Reference articles, books

Field Rails https://techracho.bpsinc.jp/hachi8833/2018_05_07/55778 https://qiita.com/sumin/items/24689d0a5622f000a71f

Recommended Posts

Do not write 〇〇 logic in Rails View (* added later)
Do not write if (isAdmin == true) code in Java
View monthly calendar in Rails
[Rails] What to do if data is not registered in DB
[Java] Do not use "+" in append!
Data is not registered in Rails.
How to write the view when Vue is introduced in Rails?
Do not use instance variables in partial
JavaScript (vanilla) does not respond in Rails.
Specify home view in rails app [root]
Do not return when memoizing in Ruby
Do not declare variables in List in Java
What to do when rails db: seed does not reflect in the database
[Rails] Various ways to write in seed files
[Japanese localization] i18n rails Easy Japanese localization view display only
Implement iteration in View by rendering collection [Rails]
ActiveModel :: MissingAttributeError (can't write unknown attribute 〇〇) in Rails
Remedy for command not found in rails s