[RUBY] Use Extend (Concerns) in Rails to standardize Controller processing.


I wanted to output the article list to Page # home. @ posts = Post.all data is in the Post model, controller. Not in Page model and controller. I was googled how to bring Post data to Page.


I want to bring this information to Page.


class PostsController < ApplicationController

    def index
      @posts = Post.all

Import to Page.


class Page < ApplicationRecord

   + extend Post::Models

Let's define Post information on Page.


def home
  @posts = Post.all

It was output.


<%= @posts.each do |p| %>
 <%= p.thumbnail %>
 <%= p.title %> 
 <%= p.tag %> 
 <%= p.content %> 
<% end %> 

There is an article that explains it better. ↓ https://github.com/mc-chinju/qiita_clone/commit/262a6178d5a2eb77c6f507cf9386cb61825bfbaf https://medium.com/@yavuz255/rake-aborted-2da1233a4561 https://railsguides.jp/active_model_basics.html

Other way ↓ How to create a file in controller/concern https://programming-beginner-zeroichi.jp/articles/142

Related words when google

extend ActiveSupport::Concern rails concern

Recommended Posts

Use Extend (Concerns) in Rails to standardize Controller processing.
[Rails] How to define macros in Rspec and standardize processing
How to use custom helpers in rails
How to use MySQL in Rails tutorial
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to use select boxes in Ransack
How to separate .scss by controller in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to use PostgreSQL in Vagrant environment
Use MouseListener in Processing
Use images in Rails
I want to use a little icon in Rails
Beginners use ubuntu in windows to prepare rails environment
Get UserAgent in [Rails] controller
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
Use multiple checkboxes in Rails6!
[Rails] How to use Scope
[Rails] How to use gem "devise"
How to use Lombok in Spring
[Rails / Routing] How to refer to the controller in the directory you created
I tried to implement Ajax processing of like function in Rails
[Rails] How to use flash messages
[Rails] How to write in Japanese
How to use Ruby on Rails
[Rails] Use cookies in API mode
[Processing × Java] How to use variables
How to use InjectorHolder in OpenAM
How to delete large amounts of data in Rails and concerns
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
How to use classes in Java?
[Introduction to Rails] How to use render
[Processing × Java] How to use arrays
How to install Swiper in Rails
Use Timecop in Rails web app to travel time on your browser
[Rails] Use devise to get information about the currently logged in user
How to debug the processing in the Ruby on Rails model only on the console
How to implement search functionality in Rails
Multilingual Locale in Java How to use Locale
How to change app name in rails
[Ruby on Rails] How to use CarrierWave
How to use named volume in docker-compose.yml
[Rails] How to use rails console with docker
How to insert a video in Rails
How to standardize header footer in Thymeleaf
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
[rails] List of actions defined in Controller
[Swift] How to use Tab Bar Controller
[Rails] Use validation on a specific controller
[Processing × Java] How to use the loop
How to use Docker in VSCode DevContainer
[Rails] Assigning variables from controller to JavaScript
Steps to set a favicon in Rails
[rails] How to configure routing in resources
[Processing × Java] How to use the class