[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 bulletin board using Docker 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
Rails Tutorial Extension: I created a follower notification function
Search function using [rails] ransack
I made a chat app.
Ajax bookmark function using Rails
I made a Japanese version of Rails / devise automatic email
I made a development environment with rails6 + docker + postgreSQL + Materialize.
I tried using Hotwire to make Rails 6.1 scaffold a SPA
[Rails] I tried to implement "Like function" using rails and js
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
Ruby: I made a FizzBuzz program!
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
I made a Ruby container image and moved the Lambda function
[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"
[Rails] Create an evaluation function using raty.js
[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.
Creating a user authentication function using devise
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
[Rails] How to handle data using enum
Multiple image upload function using Rails Carrierwave
Create a login function using Swift's Optional
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
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.
I made a Diff tool for Java files
[Rails] Implementation of search function using gem's ransack
I made a primality test program in Java
[Rails 6] Implementation of inquiry function using Action Mailer
Create authentication function in Rails application using devise
I tried barcode scanning using Rails + React + QuaggaJS