我正在使用我的应用程序,我有两个表:用户和部门。我想要做的是获取在我的表单中选择框中选择的部门的ID(我已经完成了)并向Postgres数据库查询,该部门将根据部门的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
主要想法是让用户选择他想要发送信息的部门,他将进入在所选部门工作的第二个选择框用户列表