解決したい事
- リレーションしているテーブルにも同時にデータ登録する方法(コードの書き方)が調べてもわからず困っております。
- 記事を見ても解釈できないため、わかりやすく解説している記事やサンプルコードがあれば紹介頂けたら幸いです。
accepts_nested_attributes_forを使わず、複数の子レコードを保存する
環境
- Ruby 2.6.3
- Rails 6.0.0.rc1
ruby
1#/views/users/_form.html.erb 2<%= form_with(model: user, local: true) do |form| %> 3 <% if user.errors.any? %> 4 <div id="error_explanation"> 5 <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2> 6 7 <ul> 8 <% user.errors.full_messages.each do |message| %> 9 <li><%= message %></li> 10 <% end %> 11 </ul> 12 </div> 13 <% end %> 14 15 <div class="field"> 16 <%= form.label :name %> 17 <%= form.text_field :name %> 18 </div> 19 20 <div class="field"> 21 <%= form.label :address %> 22 <%= form.text_field :address %> 23 </div> 24 25 <div class="field"> 26 <%= form.label :age %> 27 <%= form.number_field :age %> 28 </div> 29 30 <div class="actions"> 31 <%= form.submit %> 32 </div> 33<% end %>
ruby
1#/controllers/users/users_controller.rb 2class UsersController < ApplicationController 3 before_action :set_user, only: [:show, :edit, :update, :destroy] 4 5 # GET /users 6 # GET /users.json 7 def index 8 @users = User.all 9 end 10 11 # GET /users/1 12 # GET /users/1.json 13 def show 14 # @user = @user.companies.build 15 end 16 17 # GET /users/new 18 def new 19 @user = User.new 20 end 21 22 # GET /users/1/edit 23 def edit 24 end 25 26 # POST /users 27 # POST /users.json 28 def create 29 @user = User.new(user_params) 30 31 respond_to do |format| 32 if @user.save 33 format.html { redirect_to @user, notice: 'User was successfully created.' } 34 format.json { render :show, status: :created, location: @user } 35 else 36 format.html { render :new } 37 format.json { render json: @user.errors, status: :unprocessable_entity } 38 end 39 end 40 end 41 42 # PATCH/PUT /users/1 43 # PATCH/PUT /users/1.json 44 def update 45 respond_to do |format| 46 if @user.update(user_params) 47 format.html { redirect_to @user, notice: 'User was successfully updated.' } 48 format.json { render :show, status: :ok, location: @user } 49 else 50 format.html { render :edit } 51 format.json { render json: @user.errors, status: :unprocessable_entity } 52 end 53 end 54 end 55 56 # DELETE /users/1 57 # DELETE /users/1.json 58 def destroy 59 @user.destroy 60 respond_to do |format| 61 format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 62 format.json { head :no_content } 63 end 64 end 65 66 private 67 # Use callbacks to share common setup or constraints between actions. 68 def set_user 69 @user = User.find(params[:id]) 70 end 71 72 # Never trust parameters from the scary internet, only allow the white list through. 73 def user_params 74 params.require(:user).permit(:name, :address, :age) 75 end 76end
ruby
1# /models/user.rb 2class User < ApplicationRecord 3 has_many :companies 4end 5 6# /models/company.rb 7class Company < ApplicationRecord 8 belongs_to :users 9end
ruby
1# db/schema.rb 2ActiveRecord::Schema.define(version: 2019_05_17_051133) do 3 4 # These are extensions that must be enabled in order to support this database 5 enable_extension "plpgsql" 6 7 create_table "companies", force: :cascade do |t| 8 t.string "name" 9 t.datetime "created_at", precision: 6, null: false 10 t.datetime "updated_at", precision: 6, null: false 11 t.bigint "user_id", null: false 12 t.index ["user_id"], name: "index_companies_on_user_id" 13 end 14 15 create_table "users", force: :cascade do |t| 16 t.string "name" 17 t.string "address" 18 t.integer "age" 19 t.datetime "created_at", precision: 6, null: false 20 t.datetime "updated_at", precision: 6, null: false 21 end 22 23 add_foreign_key "companies", "users" 24end
あなたの回答
tips
プレビュー