[RUBY] [Rails] I want to send data of different models in a form

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.


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.references :user, foreign_key: true By setting a foreign key, the User model and User_profile model are linked.

$ rails db:migrate


class Prof < ApplicationRecord
  belongs_to :user


class User < ApplicationRecord
  has_one :user_profile
  accepts_nested_attributes_for :user_profile

ʻ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.



def create
  @user = User.new(user_params)
  if @user.save
・ ・ ・


def user_params
  params.require(:user).permit(:name, :email, :password, user_profile_attributes: [:id, :program_lang, :twitter_url, :image])

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.



  = form_with model: @user, local: true do |form|
    = form.fields_for :user_profile do |prof|
        = prof.file_field :image

      = form.text_field :name
      = form.text_field :email

    = form.fields_for :user_profile do |prof|
        = prof.text_field :program_lang
        = prof.text_field :twitter_url

      = 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

[Rails] I want to send data of different models in a form
I want to use a little icon in Rails
I want to define a function in Rails Console
I want to create a form to select the [Rails] category
[Rails] I want to display the link destination of link_to in a separate tab
I want to send an email in Java.
[For beginners] I want to automatically enter pre-registered data in the input form with a selection command.
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
Rails6 I want to make an array of values with a check box
How to make a unique combination of data in the rails intermediate table
I want to call a method of another class
I want to click a GoogleMap pin in RSpec
I want to import the pull-down menu items when submitting a form in Rails into CSV and display them from the DB data.
I want to find a relative path in a situation using Path
I want to make a specific model of ActiveRecord ReadOnly
A story I was addicted to in Rails validation settings
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
I tried to make a client of RESAS-API in Java
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
Implement a contact form in Rails
I tried to implement Ajax processing of like function in Rails
I want to add a browsing function with ruby on rails
I tried to sort the data in descending order, ascending order / Rails
[Ruby] I want to put an array in a variable. I want to convert to an array
How to delete large amounts of data in Rails and concerns
I want to add devise in Rails, but I can't bundle install
[Rails] I want to add data to Params when transitioning with link_to
I want to change the value of Attribute in Selenium of Ruby
I want to develop a web application!
I want to write a nice build.gradle
How to insert a video in Rails
I want to write a unit test!
Steps to set a favicon in Rails
I want to use @Autowired in Servlet
I tried to make a parent class of a value object in Ruby
The parameters I received in Rails were a bit different than I expected
I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
[Ruby] I want to display posted items in order of newest date
I want to display a PDF in Chinese (Korean) with thin reports
I want to ForEach an array with a Lambda expression in Java
"Teacher, I want to implement a login function in Spring" ① Hello World
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
[Order method] Set the order of data in Rails
Convert to a tag to URL string in Rails
I want to output the day of the week
I tried to organize the session in Rails
I want to use arrow notation in Ruby
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
I want to be able to read a file using refile with administrate [rails6]
[Ruby] I want to do a method jump!
I want to var_dump the contents of the intent
A memo to simply create a form using only HTML and CSS in Rails 6
I want to pass APP_HOME to logback in Gradle
[Rails] How to get rid of flash messages in a certain amount of time
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to simply write a repeating string
I want to design a structured exception handling