[Rails] I made a draft function using enum

Introduction

When I thought about making a draft function, there weren't many articles, so I decided to keep a record.

Development environment

ubuntu(WSL)
Ruby 2,5.1
Rails 6.0.2

Advance preparation

The following functions are assumed to have been created.

--Post function / details / delete / edit (Post) --Create / Details of User (User)

models/post.rb


  belongs_to :user

models/user.rb


  has_many :microposts, dependent: :destroy

Add a column to the Post table

First, add a status column to the Post model to make it a boolean type. It is possible to use interger instead of boolean. Enter the following command.

bin/rails g migration AddStatusToPost status:boolean

Edit the migration file.

migrationfile.rb


  def change
    add_column :microposts, :status, :boolean, default: true, null: false
  end

After editing, migrate.

Add enum to Post model.

models/post.rb


  enum status: { draft: false, published: true }

Specify the draft of the status column as false and the published of the status column as true.

Get information for each user

Get the id of user with @ user.

users_controller.rb


  #For draft
  def confirm
    @user = User.find(params[:user_id])
    @microposts = @user.microposts.draft.page(params[:page])
  end

  #For publication
  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.published.page(params[:page])
  end

Add routing

If you add collection, the URL will not have an id.

routes.rb


resources :users do
  get 'confirm'
end

View settings

I have extracted only the relevant parts.

view/users/show.html.slim


//User details screen
= link_to "Post list", @user
= link_to  "favorite", user_likes_path(current_user)
= link_to "Draft list", user_confirm_path(current_user)

//View my post
- if @microposts.present?
  = render "microposts/list", microposts: @microposts
- else
h4 No posts

Please write the draft list according to your preference.

view/users/confirm.html.slim


h4 draft list

table.table.table-hover
  thead.thead-default
    tr
      th = Micropost.human_attribute_name(:title)
      th = Micropost.human_attribute_name(:content)
      th = Micropost.human_attribute_name(:created_at)
      th
  tbody
    - @microposts.each do |micropost|
      tr
        td = link_to micropost.title, micropost
        td = link_to micropost.content, micropost
        td

The contents of ╩╗icroposts / list` are the same as above.

At the end

If you make a mistake, please make an edit request or comment.

References

-Now you can save draft and public articles using Rails enum

Recommended Posts

[Rails] I made a draft function using enum
I made a simple recommendation function.
I made a reply function for the Rails Tutorial extension (Part 1)
I made a reply function for the Rails Tutorial extension (Part 5):
I made a LINE bot with Rails + heroku
I made a reply function for Rails Tutorial extension (Part 2): Change model
I made a portfolio with Ruby On Rails
I want to define a function in Rails Console
Search function using [rails] ransack
I made a chat app.
I made a Japanese version of Rails / devise automatic email
I made a development environment with rails6 + docker + postgreSQL + Materialize.
I made a simple MVC sample system using Spring Boot
I made a Dockerfile to start Glassfish 5 using Oracle Java
I made a reply function for the Rails Tutorial extension (Part 4): A function that makes the user unique
Add a search function in Rails.
[Rails] Tag management function (using acts-as-taggable-on)
I made a shopify app @java
I made a GUI with Swing
Create a filtering function using acts-as-taggable-on
I made a matching app (Android app)
I made a package.xml generation tool.
[Android] I made a pedometer app.
I made a command line interface with WinMerge Plugin using JD-Core
[Rails] I made a simple calendar mini app with customized specifications.
I want to add a browsing function with ruby on rails
[Implementation procedure] Create a user authentication function using sorcery in Rails
I tried to build a simple application using Dockder + Rails Scaffold
I tried to make a group function (bulletin board) with Rails
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
[Ruby] I made a simple Ping client
[Rails] What I learned from a little stumbling block when using ancestry
[Rails withdrawal] Create a simple withdrawal function with rails
[Rails] I will explain the implementation procedure of the follow function using form_with.
I made a risky die with Ruby
Make a login function with Rails anyway
I made a plugin for IntelliJ IDEA
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a new Java deployment tool
I made a rock-paper-scissors app with android
Create a login function using Swift's Optional
I want to be able to read a file using refile with administrate [rails6]
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
Delegate pattern between views. I also made a sample page transition using NavigationLink.
[Rails] Implementation of search function using gem's ransack
I made a primality test program in Java
Create authentication function in Rails application using devise
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
[Rails] Implementation of image enlargement function using lightbox2
Generate a test account password using Rails SecureRandom.alphanumeric
Implement star rating function using Raty in Rails6
I made blackjack with Ruby (I tried using minitest)
I made a rock-paper-scissors game in Java (CLI)
I made a viewer app that displays a PDF
I made a Docker container to run Maven
Let's make a search function with Rails (ransack)
[Rails] Creating a breadcrumb trail using Gem gretel
[For beginners] Procedure for creating a controller using rails
I tried to implement a server using Netty