[Ruby on Rails] Pass the parameters divided by date_select to FormObject.

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.

Description

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. image.png

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

[Ruby on Rails] Pass the parameters divided by date_select to FormObject.
Pass parameters to Rails link_to
How to use Ruby on Rails
Things to remember and concepts in the Ruby on Rails tutorial
(Ruby on Rails6) Create a function to edit the posted content
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] Button to return to top
How to debug the processing in the Ruby on Rails model only on the console
[Rails] How to decide the destination by "rails routes"
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] Until the introduction of RSpec
Publish the app made with ruby on rails
Determine the current page with Ruby on Rails
[Ruby on Rails] Change the update date and creation date to your favorite notation
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Change URL id to column name
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
Ruby on Rails Elementary
Ruby on Rails basics
[Rails] How to get the contents of strong parameters
Ruby On Rails Association
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) Reflecting the posted content from the form
(Ruby on Rails6) How to create models and tables
Try using the query attribute of Ruby on Rails
[Ruby on Rails] Only the user who posted can edit
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
[Ruby on Rails] Quickly display the page title in the browser
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
(Ruby on Rails6) Display of the database that got the id of the database
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Automatically enter the address from the zip code
How to display a graph in Ruby on Rails (LazyHighChart)
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
[Rails] How to display the list of posts by category
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
How to run React and Rails on the same server
Apply CSS to a specific View in Ruby on Rails
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
How to make an application with ruby on rails (assuming that the environment has been built)
Ruby on Rails --From environment construction to simple application development on WSL2