写入连接表从嵌套表单

时间:2017-01-25 19:35:57

标签: ruby-on-rails parameters

我是Rails 5中的第一个应用程序(Ruby 2.4.0),我有几个问题;最大的(目前)是从嵌套表格中检索参数。

我的问题以及下面的文件:

1)首先,这是创建模型嵌套表单并在连接表中写入的正确方法吗?

2)如何从嵌套表单中的select中获取params:我无法检索city_id(来自app / models / link_patient_address_city.rb中)并将其存储在link_patient_address_cities表中。

3)如何才能获得刚刚创建的患者ID,以便在link_patient_address_cities表中写入?

感谢。

我有3张桌子:

  1. 患者(:id,:姓名)
  2. 一个JOIN表link_patient_address_cities(:id,:patient_id, :city_id)
  3. cities(:id,:name)
  4. 当我创建新患者时,我想将city_id和新创建的patient_id存储在link_patient_address_cities表中。

    这些是我的模型,控制器和表单文件:

    应用/模型/ patient.rb

    class Patient < ApplicationRecord
    has_many :link_patient_address_cities
    has_many :cities, :through => :link_patient_address_cities
    has_many :phone_contacts
    has_many :patient_emails
    def
    link_patient_address_cities_attributes=(attributes)
        # Process the attributes hash
      end
    end
    

    app / models / link_patient_address_city.rb

    class LinkPatientAddressCity < ApplicationRecord
    belongs_to :patient #, index: true
    belongs_to :city #, index: true
    
        def self.insert_recapito(p)
        l=LinkPatientAddressCity.new
        l.city_id=p[:link_patient_address_cities_attributes[:city_id]]
        l.patient_id=2
        l.save
        end
    end
    

    app / controllers / link_patient_address_cities_controller.rb

    class LinkPatientAddressCitiesController < ApplicationController
      before_action :set_link_patient_address_city, only: [:show, :edit, :update, :destroy]
    
      # GET /link_patient_address_cities
      # GET /link_patient_address_cities.json
      def index
        @link_patient_address_cities = LinkPatientAddressCity.all
      end
    
      # GET /link_patient_address_cities/1
      # GET /link_patient_address_cities/1.json
      def show
      end
    
      # GET /link_patient_address_cities/new
      def new
        @link_patient_address_city = LinkPatientAddressCity.new
      end
    
      # GET /link_patient_address_cities/1/edit
      def edit
      end
    
      # POST /link_patient_address_cities
      # POST /link_patient_address_cities.json
      def create
        @link_patient_address_city = LinkPatientAddressCity.new(link_patient_address_city_params)
    
        respond_to do |format|
          if @link_patient_address_city.save
            format.html { redirect_to @link_patient_address_city, notice: 'Link patient address city was successfully created.' }
            format.json { render :show, status: :created, location: @link_patient_address_city }
          else
            format.html { render :new }
            format.json { render json: @link_patient_address_city.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # PATCH/PUT /link_patient_address_cities/1
      # PATCH/PUT /link_patient_address_cities/1.json
      def update
        respond_to do |format|
          if @link_patient_address_city.update(link_patient_address_city_params)
            format.html { redirect_to @link_patient_address_city, notice: 'Link patient address city was successfully updated.' }
            format.json { render :show, status: :ok, location: @link_patient_address_city }
          else
            format.html { render :edit }
            format.json { render json: @link_patient_address_city.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # DELETE /link_patient_address_cities/1
      # DELETE /link_patient_address_cities/1.json
      def destroy
        @link_patient_address_city.destroy
        respond_to do |format|
          format.html { redirect_to link_patient_address_cities_url, notice: 'Link patient address city was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_link_patient_address_city
          @link_patient_address_city = LinkPatientAddressCity.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def link_patient_address_city_params
          params.require(:link_patient_address_city).permit(:last_address)
        end
    end
    

    应用/模型/ city.rb

    class City < ApplicationRecord
        belongs_to :province #, index: true
        has_many :link_patient_address_cities
        has_many :patients, :through => :link_patient_address_cities
    end
    

    /app/views/patients/_form.html.erb

    <%= form_for(patient) do |f| %>
    <%= f.label : name %>
    <%= f.text_field : name %>
    <%= f.fields_for :link_patient_address_cities do |lpac_form| %>
    <%= lpac_form.collection_select :city_id, City.order(:city_name), :city_code, :city_name, {include_blank:"Select one..."}, {class: "form-control"} %>
    <% end %>
    <% f.submit %>
    <% end %>
    

    生成的选择标记为:

    <select class="form-control" name="patient[link_patient_address_cities_attributes][0][city_id]" id="patient_link_patient_address_cities_attributes_0_city_id">
    

0 个答案:

没有答案