Updating 2 tables with one form Ruby on rails and MySQL database.
Tags: ruby
Published: 2021-04-08 13:57:30

Let's create 2 models that are related to each other...

parent model : Product with name attribute.

child model: Imaged with URL attribute.

rails g model Image url:string 

rails g scaffold Product name:string references Image



Next set-up MySQL database from this link: 

https://www.ionos.com/digitalguide/server/know-how/use-mysql-with-ruby-on-rails/

after setting up the db...


user the product controller to update both the 2 models.

like this in product controller: 

class ProductsController < ApplicationController
  before_action :set_product, only: %i[ show edit update destroy ]

  # GET /products or /products.json
  def index
    @products = Product.all
    
  end

  # GET /products/1 or /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
    @image=@product.build_image
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products or /products.json
  def create
    @product = Product.new(product_params)
    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: "Product was successfully created." }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1 or /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: "Product was successfully updated." }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1 or /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: "Product was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def product_params
      params.require(:product).permit(:name,
      image_attributes:[:url]
      )
    end
end



Next.....


update the _form.html.erb on product views:

we want to give to combine both the models with one form: 


<%= form_with(model: product) do |form| %>
  <% if product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
        <% product.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>


  <% 3.times do %>
  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name %>

  </div>
 




<%= form.fields_for :image do |form| %>
  <div class="form-group">
  <%= form.label :url %>
  <%= form.text_field :url %>
  <%= form.label :alt %>
  <%= form.text_field :alt %>
 
  
  </div>
  <% end %>
  <% end %>




  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name %>

  </div>





<%= form.fields_for :image do |form| %>
  <div class="form-group">
  <%= form.label :url %>
  <%= form.text_field :url %>
  <%= form.label :alt %>
  <%= form.text_field :alt %>
 
  
  </div>
  <% end %>




  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>


lastly: update the the 2 models:


product.rb

class Product < ApplicationRecord
  has_one :image,dependent: :destroy
  accepts_nested_attributes_for :image
end


image.rb

class Image < ApplicationRecord
  belongs_to :product
end




if you guys have question about that.. you can ask me. Goodluck.