создавать записи только во вложенных ресурсах - PullRequest
1 голос
/ 28 мая 2020

Я создаю форму, в которой я создаю связанные записи, пока она работает довольно хорошо, она создает вложенные записи, но когда я их создаю, она показывает в той же форме, что и связанные записи, созданные ранее, мне просто нужно сохранить записи и не показывать те, которые созданы в текстовых полях, как я могу это сделать? это моя форма

<%= form_with(model: drugs_device, local: true, html: {class: "formulario_validado"}) do |form| %>

  <div class="form-row">
    <div class="form-group col-md-6">
      <%= form.label :abbreviation,"Código / ATC" %>
      <%= form.text_field :abbreviation, class:"form-control", required: "true"%>
    </div>

    <%=form.fields_for :detail_drugs_devices do |fd| %>
      <div class="form-row">

        <div class="form-group col-md-3">
          <%= fd.label :drug_concentration,"Concentration:" %>
          <%= fd.text_field :drug_concentration, class:"form-control" %>
        </div>

        <div class="form-group col-md-3">
          <%= fd.label :route_id,"Vía de administración" %>
          <%= fd.select :route_id, options_for_select(@routes.map{|e|[e.description, e.id]}), {:prompt => "Por favor seleccione"}, {:class => "form-control"} %>
        </div>
      </div>    

    <%end%>

    <div class="row">
      <div class="col-md-4 offset-md-8 ">
        <%= submit_tag "Guardar", class: "btn btn-primary"%>  
      </div>
    </div>
  </div>
<%end%>

моя модель drug_device:

class DrugsDevice < ApplicationRecord
  belongs_to :group
  has_many :detail_drugs_devices

  accepts_nested_attributes_for :detail_drugs_devices, reject_if: proc { |attributes| attributes['pharmaceutical_form_id'].blank?}

end

моя модель DetailDrugsDevice

class DetailDrugsDevice < ApplicationRecord
  belongs_to :drugs_device
  belongs_to :pharmaceutical_form
  belongs_to :unit_size
  belongs_to :route
end

мой контроллер:

class DrugsDevicesController < ApplicationController
  before_action :set_drugs_device, only: [:show, :edit, :update, :destroy]

  # GET /drugs_devices
  # GET /drugs_devices.json
  def index
    #@drugs_devices = DrugsDevice.Busqueda_general(params).paginate(page: params[:page]).per_page(3) 
    @drugs_devices = DrugsDevice.all.paginate(page: params[:page]).per_page(3) 
  end

  # GET /drugs_devices/1
  # GET /drugs_devices/1.json
  def show
  end

  # GET /drugs_devices/new
  def new
    @drugs_device = DrugsDevice.new
    @drugs_device.detail_drugs_devices.build

    @grupos = Group.all
    @pharmaceutical_forms = PharmaceuticalForm.all
    @unit_sizes = UnitSize.all
    @routes = Route.all
  end

  # GET /drugs_devices/1/edit
  def edit
    @drugs_device.detail_drugs_devices.build

    @grupos = Group.all
    @pharmaceutical_forms = PharmaceuticalForm.all
    @unit_sizes = UnitSize.all
    @routes = Route.all

  end

  # POST /drugs_devices
  # POST /drugs_devices.json
  def create
    @drugs_device = DrugsDevice.new(drugs_device_params)

    respond_to do |format|
      if @drugs_device.save
        format.html { redirect_to edit_drugs_device_path(@drugs_device), notice: 'Drugs device was successfully created.' }
        format.json { render :show, status: :created, location: @drugs_device }
      else
        format.html { render :new }
        format.json { render json: @drugs_device.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /drugs_devices/1
  # PATCH/PUT /drugs_devices/1.json
  def update
    respond_to do |format|
      if @drugs_device.update(drugs_device_params)
        format.html { redirect_to edit_drugs_device_path(@drugs_device), notice: 'Drugs device was successfully updated.' }
        format.json { render :show, status: :ok, location: @drugs_device }
      else
        format.html { render :edit }
        format.json { render json: @drugs_device.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /drugs_devices/1
  # DELETE /drugs_devices/1.json
  def destroy
    @drugs_device.destroy
    respond_to do |format|
      format.html { redirect_to drugs_devices_url, notice: 'Drugs device was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Only allow a list of trusted parameters through.
    def drugs_device_params
      params.require(:drugs_device).permit(:group_id, :atc, :abbreviation, :cientific_name, :stated_at, detail_drugs_devices_attributes: [:pharmaceutical_form_id, :unit_size_id, :route_id, :drug_concentration, :id])
    end
end
...