[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.
[Ruby on Rails] How to change the column name
Pass parameters to Rails link_to
[Ruby on Rails] Use the resources method to automatically create routes.
Things to remember and concepts in the Ruby on Rails tutorial
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] How to use CarrierWave
part of the syntax of ruby ​​on rails
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
Ruby on Rails DB Tips for creating methods to reduce the load
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby On Rails] How to search the contents of params using include?
[Rails] How to decide the destination by "rails routes"
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
When the Ruby on Rails terminal rolls back
Determine the current page with Ruby on Rails
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] How to make the link destination part of the specified id
[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] Rails tutorial Chapter 14 Summary of how to implement the status feed
Looking back on the important things to pass the Ruby engineer certification exam Silver
[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] Description that allows only specific users to transition to the edit page
[Ruby on Rails] How to write enum in Japanese
How to remove the underline displayed by Rails link_to
Docker the development environment of Ruby on Rails project
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[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
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[Ruby on Rails] How to stop when Rails server cannot be stopped by Ctrl + C
[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] How to search and save the data of the parent table from the child table
[Ruby on Rails] Only the user who posted can edit
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
[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
From Ruby on Rails error message display to Japanese localization
Delete all the contents of the list page [Ruby on Rails]
[Ruby on Rails] Implement login function by add_token_to_users with API
How to display a graph in Ruby on Rails (LazyHighChart)