[Ruby on Rails] Common processing between controllers (using concaves)

Introduction

As the number of site pages increases, the description of the same controller will increase. If you use concerts at that time, the description will be cleaner and the readability will be improved.

Actual code

app/controllers/posts_controller.rb


class PostController < ApplicationController
  before_action :set_posts
  def set_posts
    @posts = Post.all
  end
end

app/controllers/users_controller.rb


class UserController < ApplicationController
  before_action :set_posts
  def set_posts
    @posts = Post.all
  end
end

As mentioned above, there is a common process called set_posts. However, if there is a change, both must be corrected, which is a hassle. The way to solve these is to use concerns.

Creating a concerns file

Create a file under app / controllers / concerts. This time, the file name will be "postable.rb".

app/controllers/concerns/postable.rb


module Postables
extend ActiveSupport::Concern

  def set_posts
    @posts = Post.all
  end

end

And to call each controller include postablesIt is described as.

app/controllers/posts_controller.rb


class PostController < ApplicationController
  include Postables
  before_action :set_posts
end

app/controllers/users_controller.rb


class UserController < ApplicationController
  include Postables
  before_action :set_posts
end

This is OK.

Summary

By doing these, even if there is a correction, you only need to correct it in one place, and you can respond immediately even if an error occurs. It is recommended to use it because the lack of description leads to maintainability.

Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork

Recommended Posts

[Ruby on Rails] Common processing between controllers (using concaves)
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] Image slideshow using Skippr
[Rails] Inherit parameters between controllers using Sessions Helper
Implement common processing using method swizzling on iOS
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
Try using the query attribute of Ruby on Rails
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Ruby on Rails Email automatic sending function setting (using gmail)
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
[Ruby on Rails] Infinite scrolling using gem kaminari and jscroll
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
Install Ruby 2.5 on CentOS 7 using SCL
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to debug the processing in the Ruby on Rails model only on the console
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] 1 model CRUD (Routing Main)