Javascript Rails:如何在select中使用jQuery显示PGDB中的值

Javascript Rails:如何在select中使用jQuery显示PGDB中的值,javascript,jquery,ruby-on-rails,database,postgresql,Javascript,Jquery,Ruby On Rails,Database,Postgresql,我正在使用我的应用程序,我有两个表:用户表和部门表。我想做的是在我的表单中的选择框中获取所选部门的ID,我已经这样做了,并向Postgres db查询,它将根据部门ID选择属于所选部门的用户。我在用户表中有外键,该表包含有关用户部门的信息。所选用户应显示在另一个选择框中,以便真实用户轻松选择 我的问题是,如何让它在Javascript/jQuery中工作?这是代码的一部分,我知道这是不必要的: code.js只有从选择框中获取值,正如我所说: document.addEventListener(

我正在使用我的应用程序,我有两个表:用户表和部门表。我想做的是在我的表单中的选择框中获取所选部门的ID,我已经这样做了,并向Postgres db查询,它将根据部门ID选择属于所选部门的用户。我在用户表中有外键,该表包含有关用户部门的信息。所选用户应显示在另一个选择框中,以便真实用户轻松选择

我的问题是,如何让它在Javascript/jQuery中工作?这是代码的一部分,我知道这是不必要的:

code.js只有从选择框中获取值,正如我所说:

document.addEventListener("turbolinks:load", function() {
 var value = $( "#departmentselect, option:selected" ).val();
});
index.html.erb:

    <%= f.collection_select :dzial_id, Dzial.all, :id, :nazwa, prompt: "Wybierz", id:"departmentselect"%>
就这些。如果有人能告诉我如何应对,我将不胜感激

编辑:

这里是zgloszenies_controller.rb

class ZgloszeniesController < ApplicationController
  before_action :set_zgloszeny, only: [:show, :edit, :update, :destroy, :print]

  # GET /zgloszenies
  # GET /zgloszenies.json
  def index
    @zgloszenies = Zgloszenie.where("nazwa_urzadzenia ILIKE ?", "%#{params[:search]}%")
  end

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

  def print
    @zgloszenie = Zgloszenie.find(params[:id])
  end

  # GET /zgloszenies/new
  def new
    @zgloszeny = Zgloszenie.new
  end

  # GET /zgloszenies/1/edit
  def edit
  end

  # POST /zgloszenies
  # POST /zgloszenies.json
  def create
    @zgloszeny = Zgloszenie.new(zgloszeny_params)

    respond_to do |format|
      if @zgloszeny.save
        format.html { redirect_to @zgloszeny, notice: 'Zgloszenie was successfully created.' }
        format.json { render :show, status: :created, location: @zgloszeny }
      else
        format.html { render :new }
        format.json { render json: @zgloszeny.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /zgloszenies/1
  # PATCH/PUT /zgloszenies/1.json
  def update
    respond_to do |format|
      if @zgloszeny.update(zgloszeny_params)
        format.html { redirect_to @zgloszeny, notice: 'Zgloszenie was successfully updated.' }
        format.json { render :show, status: :ok, location: @zgloszeny }
      else
        format.html { render :edit }
        format.json { render json: @zgloszeny.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /zgloszenies/1
  # DELETE /zgloszenies/1.json
  def destroy
    @zgloszeny.destroy
    respond_to do |format|
      format.html { redirect_to zgloszenies_url, notice: 'Zgloszenie was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def zgloszeny_params
      params.require(:zgloszenie).permit(:dzial_id, :data_zgloszenia, :opis_uszkodzenia, :nazwa_urzadzenia, :user_id, :wysylka, :priorytet, :status, :data_naprawy, :pracownikid)
    end
end

其主要思想是让用户选择他想要发送信息的部门,他将在第二个选择框中获得在所选部门工作的用户列表

请发布您的控制器代码,该代码将根据值返回响应。我通过编辑post添加了该代码;