如何引用并将多个User_ID保存到单个表单并在索引/显示页面Rails 4 App中显示所述ID

嘿所有我正在Rails 4 Ruby 2.2中构建一个CAD应用程序;

在我的通话表单中,我有一个允许调度员选择最多4个单元发送到呼叫的方框。 (见下面的布局)。

这个问题的简短之处在于找出如何保存和访问可在Users模型中找到的4个独立user_id,并将它们存储在Calls模型中,以便它们可以链接并显示在我的Show.html.erb中单位1-4(如果需要所有这些单位)

这是forms:

 

Units Responding

Unit #1 Time On Scene Time Clear
true } ) %>

Unit #2 Time On Scene Time Clear
false } ) %>

Unit #3 Time On Scene Time Clear
false } ) %>

Unit #4 Time On Scene Time Clear
false } ) %>

我最初将此作为我的collection_select代码:

  true } ) %> 

这只更新了user_id而不是unit_1,2,3,4,如下所示。

使用当前的collection_select,它更新了我通过Rails控制台validation的Postgresql DB中的用户编号(见下文)

Rails控制台输出:

 unit_1: "1", unit_2: "2", unit_3: "3", unit_4: "1", user_id: "1" 

这里的问题是不能为所有4个单元注入user_id。 并且当我尝试在显示页面上连接它以显示每个employee_ident号时,仅显示user_id:“1”。 要显示这个,我在Show.html.erb中使用以下内容

  

当我使用上面的内容时,它会返回user_id的employee_ident:“1”,当我需要它返回每个所选选项的每个employee_ident时。

这是我的展示页面:

 

Units Responding

Unit #1 Time On Scene Time Clear

Unit #2 Time On Scene Time Clear

Unit #3 Time On Scene Time Clear

Unit #4 Time On Scene Time Clear

下面我已经在Calls Controller中添加了你需要它的参考。

呼叫控制器

 class CallsController < ApplicationController before_action :set_call, only: [:show, :edit, :update, :destroy] # GET /calls # GET /calls.json def index @calls = Call.all @active_calls = @calls.select{|x| x.status == 'ACTIVE'} @pending_calls = @calls.select{|x| x.status == 'PENDING'} end # GET /calls/1 # GET /calls/1.json def show end # GET /calls/new def new @call = Call.new end # GET /calls/1/edit def edit @call = Call.find(params[:id]) end # POST /calls # POST /calls.json def create @call = Call.new(call_params) respond_to do |format| if @call.save format.html { redirect_to @call, notice: 'Call was successfully created.' } format.json { render :show, status: :created, location: @call } else format.html { render :new } format.json { render json: @call.errors, status: :unprocessable_entity } end end end # PATCH/PUT /calls/1 # PATCH/PUT /calls/1.json def update respond_to do |format| if @call.update(call_params) format.html { redirect_to @call, notice: 'Call was successfully updated.' } format.json { render :show, status: :ok, location: @call } else format.html { render :edit } format.json { render json: @call.errors, status: :unprocessable_entity } end end end # DELETE /calls/1 # DELETE /calls/1.json def destroy @call.destroy respond_to do |format| format.html { redirect_to calls_url, notice: 'Call was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_call @call = Call.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def call_params params.require(:call).permit(:call_time, :status, :primary_type, :secondary_type, :site, :address, :unit_1, :unit_2, :unit_3, :unit_4, :call_details, :unit_on_scene, :unit_clear, :call_num, :site_id, :user_id, :unit2_os, :unit2_cl, :unit3_os, :unit3_cl, :unit4_os, :unit4_cl) end end 

用户模型:

 class User < ActiveRecord::Base # Include default devise modules. Others available are: # and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :lockable, :timeoutable end 

通话模型:

 class Call < ActiveRecord::Base has_many :users end 

这是一个简单的图表,我想在这里尝试,但不知道如何使这项工作.. 在此处输入图像描述

这是Calls Schema表:

 create_table "calls", force: :cascade do |t| t.datetime "call_time" t.string "status" t.string "primary_type" t.string "secondary_type" t.string "address" t.string "call_details" t.time "unit_on_scene" t.time "unit_clear" t.integer "site_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.time "unit2_os" t.time "unit2_cl" t.time "unit3_os" t.time "unit3_cl" t.time "unit4_os" t.time "unit4_cl" t.integer "call_number", default: "nextval('call_number_seq'::regclass)" t.integer "unit_1" <-- References User_id '1' t.integer "unit_2" <-- References User_id '2' t.integer "unit_3" <-- References User_id '3' t.integer "unit_4" <-- References User_id '4' t.integer "user_id" <-- Only Stores one User_id?? 

用户表的架构:

  create_table "users", force: :cascade do |t| t.string "f_name" t.string "l_name" t.date "dob" t.string "address" t.string "city" t.string "prov" t.string "postal_code" t.string "tel" t.date "start_date" t.string "position" t.string "employee_ident" t.boolean "super_user" t.boolean "admin" t.boolean "dispatch" t.boolean "patrol" t.boolean "locked" t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.inet "current_sign_in_ip" t.inet "last_sign_in_ip" t.integer "failed_attempts", default: 0, null: false t.string "unlock_token" t.datetime "locked_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false 

我在这里有关于这个问题的类似post,但它更多地涉及选择框 – >这更像是一个进展,以找出如何显示存储在Unit_1,2,3,4列中的employee_ident(整数)在我的数据库中。

任何帮助都会很棒,因为我似乎找不到其他文章或有此问题的用户。

提前致谢。

这是通过创建连接表来完成的。 感谢所有查看此页面的人。

Interesting Posts