I want to associate models with a 1: 1 relationship so that each model data can be sent to the form at the same time. In other words, the purpose of this time is to be able to handle two data models in the form method.
This time, we will make the User model and User_profile model a 1: 1 relationship so that both user information can be edited at the same time in the form method.
M
Migration file
class CreateUserProfiles < ActiveRecord::Migration[6.0]
def change
create_table :user_profile do |t|
t.string :twitter_url
t.string :program_lang
t.string :image
t.references :user, foreign_key: true
t.timestamps
end
end
end
t.references :user, foreign_key: true
By setting a foreign key, the User model and User_profile model are linked.
$ rails db:migrate
models/user_profile.rb
class Prof < ApplicationRecord
belongs_to :user
end
models/user.rb
class User < ApplicationRecord
has_one :user_profile
accepts_nested_attributes_for :user_profile
end
ʻAccepts_nested_attributes_for: user_profileis for nested form data. Specifically, it can be summarized in the form of
{user: {user_profile_attributes: {program_lang: 〇〇, twitter_url: 〇〇, image: 〇〇}}}` at the time of strong parameters.
C
users_controller.rb
def create
@user = User.new(user_params)
@user.build_user_profile
if @user.save
・ ・ ・
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, user_profile_attributes: [:id, :program_lang, :twitter_url, :image])
end
What you should pay attention to here is @ user.build_user_profile
.
If you have not created an instance of the user_profile model in advance, the form on the View side will not be displayed.
V
ruby:edit.html.erb
.container
= form_with model: @user, local: true do |form|
= form.fields_for :user_profile do |prof|
.form-image
= prof.file_field :image
.form-group
= form.text_field :name
= form.text_field :email
= form.fields_for :user_profile do |prof|
.form-group
= prof.text_field :program_lang
= prof.text_field :twitter_url
.row
= form.submit "Edit"
It is fields_for
that recognizes yet another model data in the form.
By creating a form inside the form, you can send the data to the server side in a batch just by pressing the edit button
.
Recommended Posts