Create data of a form when saving those of another form

0

I need that when sending the data of a form, automatically send the data of another form creating a record in another table in the database, it is worth noting that both records do not have a direct relationship.

these would be the two forms

<%= form_for(@relcliruta, remote: true, html: {class: "form-horizontal"}) do |f| %> <!--ajax remote: true-->


                  <div class="field">
                    <%= f.hidden_field :IdRuta, value: params[:id]  %>
                  </div>
                  <div class="field">
                    <%= f.hidden_field :IdCliente, value: cliente.id%>
                  </div>
                  <div class="field">
                    <%= f.hidden_field :IdEmpresa, value: current_usuario.empresa_id %>

                  </div>

                </div>

                <%= submit_tag "Añadir", class: "btn btn-warning btn-xs"%>
        <%end%>

        <%= form_for(@reldayc, remote: true, html: {class: "form-horizontal"}) do |f| %> <!--ajax remote: true-->
          <%= f.hidden_field :RutaId, value: params[:id]  %>
          <%= f.hidden_field :CodCli, value: cliente.id%>
          <%= f.hidden_field :IdEmpresa, value: current_usuario.empresa_id %>

        <%end%>

I need that when creating one, the other one is created.

    
asked by LuisC 05.11.2016 в 05:48
source

1 answer

1

One way would be using Javascript. But the question has the tag rails , so I'll tell you how to do it in Rails. (Horacio and Alter Lagos have already advanced in the comments).

The Form:

NOTE: In your example you are mixing two Rails helpers. You use the form_for but on the button you use submit_tag instead of f.submit .

<%= form_tag route_creators_path, method: :post do %>
  <%= hidden_field_tag :IdRuta, params[:id]  %>
  <%= hidden_field_tag :IdCliente, cliente.id%>
  <%= hidden_field_tag :IdEmpresa, current_usuario.empresa_id %>
  <%= submit_tag "Añadir", class: "btn btn-warning btn-xs"%>
<%end%>

Driver and route:

In the example I'm using the create action of a RouteCreatorsController . You could put the action where it seems to you.

#routes.rb
resources :route_creators, only: :create


# route_creators_controller.rb
class RouteCreatorsController < ApplicationController
  def create
    @route_id = params[:IdRute]
    @cliente_id = params[:IdCliente]
    @empresa_id = params[:IdEmpresa]
    #Me invento el nombre de los Objetos porque solo tengo el nombre de variable
    #Crea primer objeto
    RelCliRuta.create(
      IdRuta: @route_id,
      IdCliente: @cliente_id,
      IdEmpresa: @empresa_id
    )
    # Crea segundo objeto
    RelDayc.create(
      RutaId: @route_id,
      CodCli: @cliente_id,
      IdEmpresa: @empresa_id
    )
    redirect_to donde_quieras_ir_path
  end
end

With this you achieve your goal. In any case whenever you start using form_tag or nested_attributes, I recommend you use a form_object . Below is a simplified example:

#routes.rb
resources :route_creators, only: :create


# route_creators_controller.rb
class RouteCreatorsController < ApplicationController
  def create
    route_creator = RouteCreator.new(params[:route_id],
      params[:client_id],
      params[:company_id])
    route_creator.create_multiple

    redirect_to donde_quieras_ir_path
  end

end

class RouteCreator
  def initialize(route_id, client_id, company_id)
    @route_id = route_id
    @client_id = client_id
    @company_id = company_id
  end

  def create_multipe
    RelCliRuta.create(
      IdRuta: @route_id,
      IdCliente: @client_id,
      IdEmpresa: @company_id
    )
    RelDayc.create(
      RutaId: @route_id,
      CodCli: @client_id,
      IdEmpresa: @company_id
    )
  end
end
    
answered by 09.03.2017 в 21:51