1. Conclusion </ b>
2. What is a Form object? </ B>
3. How to use </ b>
4. What I learned from here </ b>
Use Form object </ b>!
It's a way to save to multiple tables at once! You have to create a controller and model migrate file for the table you want to save. In this case, you can easily do it by creating a single file with the model!
❶ Create a folder dedicated to the form object in app and a file in it. And write as follows. This time, I want to reflect the purchased information and address in the DB, so I will use sellitem_address!
app/forms/sell_item_address.rb
class SellitemAddress
include ActiveModel::Model
attr_accessor :postal_code, :shipping_orig_id, :city, :address_other, :building_name,
:telephone_num, :sell_item_id, :user_id, :buy_item_id
end
: user_id is the newly registered user, : buy_item_id is the item for sale.
include By describing ActiveModel :: Model and including it, you can treat it like a model and describe validation here!
attr_accessor is a method that allows you to change the description inside a method when it is outside the method! In other words, it can be handled as an instance variable, and the getter and setter are internally processed!
❷ Enter information for the model you want to use.
app/forms/sellitem_address.rb
def save
sell_item = SellItem.create(user_id: user_id, buy_item_id: buy_item_id)
Address.create(postal_code: postal_code, shipping_orig_id: shipping_orig_id, city: city, address_other: address_other, building_name: building_name,telephone_num: telephone_num, sell_item_id: sell_item.id )
end
By doing this, we have set the columns that can be saved in the DB! user_id buy_item_id is I have already set the id of user and the id of buy_item, so I want to extract each id as it is!
❸ Set to instruct the controller to register the information entered in the view in the DB.
sell_items_controller
def create
@sell_item = SellitemAddress.new(sell_item_params)
if @sell_item.save
return redirect_to root_path
else
render 'sell_items/index'
end
private
def sell_item_params
params.permit(:postal_code, :shipping_orig_id, :city, :address_other, :building_name, :telephone_num, :user_id, :buy_item_id).merge(user_id: current_user.id)
end
end
In the sell_item_params method, the information set in the model is described and summarized as sell_item_params when saving it. This is very useful as it will determine which table each column is stored in! Since the number of descriptions to be read is reduced, it is clear without imposing a load on the operation!
Originally, I tried to create the controller, model and table for which I want to save the migrate file, and link it with view. In addition, it is necessary to instruct each controller in each input part whether to cooperate with each controller. There is a good chance that an error will occur even in the extensibility part. This method improves maintainability and readability, and does not impose a load on the operation rather than giving instructions to each controller!
Recommended Posts