Caio Felipe
Caio Felipe

Reputation: 81

How to Insert data in two tables from one form

I'm new in RoR and i'm searching for a way to insert data from one form into two different tables, in a single insert. I tried to use accepts_nested_attributes_for

Database Association, trying to make it work with this two tables

class Morning < ApplicationRecord
has_many :foots
accepts_nested_attributes_for :foots  
end

class Foot < ApplicationRecord
belongs_to :morning
end

View form using bootstrap_form gem, i don't know if this model of code will work for this purpose

<%= bootstrap_form_with(model: @morning, url: morning_path, local: true) do |form| %>

<%= form.text_field :name, placeholder: "Nome", hide_label: true, required: true%>

<%= form.select :hour, [["Escolha um Horário", 0], ["06:00 às 06:30", "um"], ["06:30 às 
07:00", "dois"], ["07:00 às 07:30", "tres"]], { hide_label: true, required: true, wrapper: { class: 'has- 
warning', data: { foo: 'bar' } } }, { class: "selectpicker" } %>

<%= form.fields_for :foot do |foot| %>
        <%= foot.select :hour, [["Escolha um Horário", 0], ["06:00 às 06:30", "um"], ["06:30 
às 07:00", "dois"], ["07:00 às 07:30", "tres"]], { hide_label: true, required: true, wrapper: { class: 'has- 
warning', data: { foo: 'bar' } } }, { class: "selectpicker" } %>
    <% end %>

<%= form.submit "Registrar horário", class:"btn btn-primary", data: { disable_with: 
'Registrando....' } %>

<% end %>

Controller

class MorningController < ApplicationController

def new
    @morning = Morning.new
    @foot = @morning.foots.new 
end

def create
    @morning = Morning.create(morning_params)

        ActiveRecord::Base.transaction do
            @foot = @morning.foots.create(morning_params)
        end

    redirect_to morning_path
end

private
def morning_params
    params.require(:morning).permit(:name, :hour, foot_attributes: [ :name, :hour ])
end

end

Upvotes: 1

Views: 142

Answers (1)

code_aks
code_aks

Reputation: 2074

I have seen that there is a has_many relationship so change in form and in controller foot ==> foots

As in below code:-

<%= bootstrap_form_with(model: @morning, url: morning_path, local: true) do |form| %>
.....
....

<%= form.fields_for :foots do |foot| %>
        <%= foot.select :hour, [["Escolha um Horário", 0], ["06:00 às 06:30", "um"], ["06:30 
às 07:00", "dois"], ["07:00 às 07:30", "tres"]], { hide_label: true, required: true, wrapper: { class: 'has- 
warning', data: { foo: 'bar' } } }, { class: "selectpicker" } %>
    <% end %>
.....
....
<% end %>

Then in controller

def create
  @morning = Morning.new(morning_params)

  if @morning.save
    flash[:notice] = "Your custom message."
  else
    flash[:alert] = "Your custom message."
  end
  redirect_to morning_path
end

private

def morning_params
    params.require(:morning).permit(:name, :hour, foots_attributes: [ :name, :hour ])
end

### Edit To add name field in child form

    <%= bootstrap_form_with(model: @morning, url: morning_path, local: true) do |form| %>

  <!-- Morning name in morning table -->
  <%= form.text_field :name, placeholder: "Morning name", hide_label: true, required: true%> 

  <%= form.select :hour, [["Escolha um Horário", 0], ["06:00 às 06:30", "um"], ["06:30 às 
     07:00", "dois"], ["07:00 às 07:30", "tres"]], { hide_label: true, required: true, wrapper: { class: 'has- 
     warning', data: { foo: 'bar' } } }, { class: "selectpicker" } %>

  <%= form.fields_for :foots do |foot| %>

    <!-- Foot name in foots table -->
    <%= foot.text_field :name, placeholder: "Foot name", hide_label: true, required: true%>

    <%= foot.select :hour, [["Escolha um Horário", 0], ["06:00 às 06:30", "um"], ["06:30 
     às 07:00", "dois"], ["07:00 às 07:30", "tres"]], { hide_label: true, required: true, wrapper: { class: 'has- 
     warning', data: { foo: 'bar' } } }, { class: "selectpicker" } %>

  <% end %>

  <%= form.submit "Registrar horário", class:"btn btn-primary", data: { disable_with: 
  'Registrando....' } %>

<% end %>

Upvotes: 1

Related Questions