Deepened my understanding of the merge method

Introduction

In the process of implementing the function to save information from the form to multiple tables using the form object, record the most troubled error so that you do not forget it.

Assumed scene

The user purchases the item. At the same time as saving what you entered in the form in the database, it also saves which product was purchased by which user. That is, when you press "Buy" on the form, it will be saved in two tables.

About the merge method

The method to use when setting strong parameters.

Example of use

controllers.rb


private
  def user_order_params
    params.require(:user_order).permit(:postal_code, :prefecture_id).merge(user_id: current_user.id, item_id: params[:item_id])
  end

ʻUser_id: The current_user method of current_user.id can be used because the devise Gem is installed. You can enter values with ʻitem_id: params [: item_id] because the routing is nested and the URL contains ʻitem_id`. Strong parameters are described below the private method.

The argument of require is the model name. The argument of permit is the column name of DB.

Parameters that can be confirmed in the terminal

 "user_order"=>{"hoge"=>"", "postal_code"=>"", "prefecture_id"=>"1"}, "commit"=>"Purchase", "controller"=>"orders", "action"=>"create", "item_id"=>"7"}

When using the merge method

When the content entered by the user in form_with is included in the hash together with the key, but you want to save the content not entered by the user. For example, the user's id and the id of the product are not directly entered by the user, but I want to include them in the parameters and save the DB. In such a case, use the merge method to describe the key and value you want to include in the parameter. In the above example, user_id is taken from crrent_user.id and item_id is taken from params included in the URL and included in the parameters.

Finally

When the error was resolved, he muttered Marge alone ...

Recommended Posts

Deepened my understanding of the merge method
About the role of the initialize method
merge method
My thoughts on the equals method (Java)
The process of understanding Gemfile by non-engineers
[Order method] Set the order of data in Rails
[Java] Handling of JavaBeans in the method chain
[RxSwift] I want to deepen my understanding by following the definition of Observable
The order of Java method modifiers is fixed
Log4j 2 Understanding the underlying concepts of the log system
Output of how to use the slice method
[Java] When writing the source ... A memorandum of understanding ①
Are you using the default method of the interface properly?
Output about the method # 2
The world of clara-rules (2)
A story that deepened the understanding of devise's methods user_signed_in? And current_user through error resolution
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
About the length method
definition of ruby method
The world of clara-rules (5)
The idea of quicksort
About the authenticate method.
About the map method
About the ancestors method
About the to_s method.
The idea of jQuery
The permission specification of the FileUtils method is an octal number.
Is the method of the primitive specialized IntFunction apply or applyAsInt?
Now, I understand the coordinate transformation method of UIView (Swift)
[Ruby] Questions and verification about the number of method arguments
I want to expand the clickable part of the link_to method
From the habit of creating Value Objects for object-oriented understanding
Summary of values returned by the Spliterator characteristics method #java
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
Aiming for a basic understanding of the flow of recursive processing