I think that FormObject is used when you want to update multiple models by submitting one form. (If you want to know about FormObject, click here: [Rails] Please use FormObject)
I was in trouble because I couldn't pass the parameters divided by date_select
to that FormObject as it is. After declaring ʻinclude ActiveRecord :: AttributeAssignment` to FormObject, I was able to pass them all together, so I would like to introduce them below. I will.
The situation is when you register your birthday on the new registration screen.
Press the CreateUser
button on the image to run the controller's create action. Divided parameters using the StrongParameter mechanism in the user_params method <ActionController :: Parameters {"birthday (1i) "=>" 2020 "," birthday (2i) "=>" 7 "," birthday (3i) "=>" 13 "} permitted: Get true>
.
app/controllers/users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = Form.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = Form.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:birthday)
end
end
app/forms/form.rb
class Form
include ActiveModel::Model
include ActiveModel::Attributes
attribute :birthday, :date
def to_model
User.new(birthday: birthday)
end
def save
return false if invalid?
to_model.save
end
end
As stated in @user = Form.new (user_params)
,
If you try to pass the divided parameters by the initial value of FormObject, an error will occur because it is divided. <ActionController :: Parameters {"birthday (1i) "=>" 2020 "," birthday (2i) "=>" 7 "," birthday (3i) "=>" 12 "} permitted: true>
I want to pass it as a birthday parameter.
So, if you declare ʻinclude ActiveRecord :: AttributeAssignment`, it will pass the divided parameters together to attribute. case being settled.
app/forms/form.rb
class Form
include ActiveModel::Model
include ActiveModel::Attributes
#add to
include ActiveRecord::AttributeAssignment
attribute :birthday, :date
def to_model
User.new(birthday: birthday)
end
def save
return false if invalid?
to_model.save
end
end
Recommended Posts