[Rails] Use validation on a specific controller

Introduction

I'll write about what to do if you want a particular validation to work only on a particular controller.

Concrete example

For example, when registering user information, consider when you want to register information step by step.

Users table

column type
name string
email string
password string
address string
image string

Page 1: name, email, password 2nd page: address, image

Consider the process of saving name, email, and password, and then saving adress and image.

If you want to make all columns mandatory, just write presence: true in the model and you will get an error when registering the first page. (Because the address and image are empty)

What should I do if I want to use different validations on the first and second pages?

manner

You can use with_options for each action. The image is like giving the model a name for the validation group as with_options on: ~ do and checking the validation using thevalid? (Name)method on the controller.

The specific writing method is as follows. (Please note that some parts are written with a slight break.)

model


with_options on: :step1 do
  validates :name, presence: true
  validates :email, uniqueness: true
  validates :password, presence: true, length: { minimum: 8 }
  validates :password, confirmation: true
end

with_options on: :step2 do
  validates :address, presence: true
  validates :image, uniqueness: true
end

step1 controller


def create
  @user = User.new(user_params)
  if @user.valid?(:step1) && @user.save
    return redirect_to step2_path
  end

  render :new
end

step2 controller


def update
  @user = User.find(params[:id])
  if @user.valid?(:step2) && @user.update(user_params)
    return redirect_to user_path
  end

  render :edit
end

Supplement

If you want to apply all validations at the same time, you need to write as valid? (: Step1) && valid? (: Step2), which is a little troublesome ... (I thought that all with_options would be used just by valid ?, but it didn't work)

By the way, if you want to use create and update properly, please refer to the article here. (Although there is a part that overlaps with this post ...)

Recommended Posts

[Rails] Use validation on a specific controller
Post a video on rails
[Rails] How to use validation
Ruby on Rails validation summary
Apply CSS to a specific View in Ruby on Rails
How to use Ruby on Rails
A memorandum on how to use Eclipse
Rails: 7 basic actions defined on the controller
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] model, controller terminal command
Use npm while using Sprockets on Rails 5
Try deploying a Rails app on EC2-Part 1-
[Ruby on Rails] Controller test with RSpec
Ruby on Rails controller create / delete command
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
When you want Rails to disable a session for a specific controller only
Validation settings for Ruby on Rails login function
Add a tag function to Rails. Use acts-as-taggable-on
Use ControlsFX / Validation
Tailwind on Rails
[rails] Set validation
Introducing Rspec, a Ruby on Rails test framework
[Ruby on Rails] A memorandum of layout templates
(Ruby on Rails6) Creating data in a table
[For beginners] Procedure for creating a controller using rails
[Ruby on Rails] How to use session method
[Rails5] Rspec -validation-
About Rails controller
[Rails] Use jQuery
I made a portfolio with Ruby On Rails
A story comparing front frameworks on existing Rails applications
Build a Ruby on Rails development environment on AWS Cloud9
Use Extend (Concerns) in Rails to standardize Controller processing.
Use Vue.js on a CDN (only copy and paste!)
I want to use a little icon in Rails
How to make JavaScript work on a specific page
Use java1.7 (zulu7) under a specific directory with jenv
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
A high bill from AWS during development on rails ...
[Rails] How to load JavaScript in a specific view
[Rails 6] Validation by belongs_to
Launch Rails on EC2
Use perltidy on CentOS 8
Use Flutter on Ubuntu
[Rails] Customize devise validation
Use mod_auth_cas on CentOS 8
Deploy RAILS on EC2
Ruby on Rails Elementary
Ruby on Rails basics
Use images in Rails
Use bat on Centos.
Yay! I'm on Rails!
Use mkdir on ubuntu
Use Corretto 11 on Heroku
Use cpplapack on ubuntu
Ruby On Rails Association
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
[Ruby on Rails] Add a column with a foreign key constraint
A note about the seed function of Ruby on Rails
A story I was addicted to in Rails validation settings