如何引用和保存多个User_ID到一个表单中,并在索引中显示所说的ID /显示Rails 4 App的页面 [英] How to Reference and Save Several User_ID's to a single form and display said Id's in index / Show Pages of Rails 4 App

查看:68
本文介绍了如何引用和保存多个User_ID到一个表单中,并在索引中显示所说的ID /显示Rails 4 App的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我要在Rails 4 Ruby 2.2中构建一个CAD应用程序;

Hey all I'm building out a CAD App in Rails 4 Ruby 2.2;

在我的通话表单中,我有一个框,允许调度员选择最多4个单位发送呼叫。 (有关布局,请参见下文)。

in my Calls form I have a box that allows the dispatcher to select up to 4 units to send to a call. (see below for layout).

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

The Short and Narrow of this question is to find out how to save and access 4 independent user_id's that would be found in the Users model and store them in the Calls model so they can be linked and displayed in my Show.html.erb as respective units 1-4 (if all those units are required)

这是以下形式:

<%= form_for(@call) do |f| %>
    <div class="panel panel-success" id="responding-box">
      <div class="panel-heading"><center><h4>Units Responding</h4></center></div>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #1</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><center><%= f.collection_select(:unit_1, User.all, :id, :employee_ident, {}, { :multiple => true } ) %></center></td>
            <td><center><%= f.time_select :unit_on_scene %></center></td>
            <td><center><%= f.time_select :unit_clear %></center></td>
          </tr>
        </tbody>
      </table>
      <br>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #2</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><center><%= f.collection_select(:unit_2, User.all, :id, :employee_ident, {}, { :multiple => false } ) %></center></td>
            <td><center><%= f.time_select :unit2_os %></center></td>
            <td><center><%= f.time_select :unit2_cl %></center></td>
          </tr>
        </tbody>
      </table>
      <br>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #3</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><center><%= f.collection_select(:unit_3, User.all, :id, :employee_ident, {}, { :multiple => false } ) %></center></td>
            <td><center><%= f.time_select :unit3_os %></center></td>
            <td><center><%= f.time_select :unit3_cl %></center></td>
          </tr>
        </tbody>
      </table>
      <br>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #4</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
          <td><center><%= f.collection_select(:unit_4, User.all, :id, :employee_ident, {}, { :multiple => false } ) %></center></td> 
            <td><center><%= f.time_select :unit4_os %></center></td>
            <td><center><%= f.time_select :unit4_cl %></center></td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
<% end %>

我最初将其作为我的收藏集选择代码:

I originally had this as my collection_select code:

<td><center><%= f.collection_select(:call, :user_id, User.all, :id, :employee_ident, {}, { :multiple => true } ) %></center></td>

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

This only updated the user_id and not unit_1,2,3,4 as displayed below.

使用当前collection_select,它将更新我已通过Rails Console验证的Postgresql数据库中的用户号(见下文)

With the current collection_select, it updates the user number in the Postgresql DB which i have verified through Rails Console (see below)

Rails控制台输出:

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

问题这是所有4个单位的用户ID都不相等。当我尝试在显示页面上连接以显示每个employee_ident编号时,仅显示user_id: 1。为了显示此内容,我在Show.html.erb

The problem here is that is dose not refrence the user_id for all 4 units. and only displays user_id: "1" when I try to connect it on the display page to show each employee_ident number. To display this I am using the following in my Show.html.erb

<td><center><%= @call.user.employee_ident %></center></td>

当我使用上面的代码时,当我需要它返回user_id的employee_ident: 1时每个所选选项的每个employee_ident。

when I use the above it returns the employee_ident of user_id: "1" when I need it to return each employee_ident for each of the selected options.

这是我的显示页面:

<div class="panel panel-success" id="responding-box">
      <div class="panel-heading"><center><h4>Units Responding</h4></center></div>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #1</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><center><%= @call.user.employee_ident %></center></td>
            <td><center><%= @call.unit_on_scene.strftime("%H:%m") %></center></td>
            <td><center><%= @call.unit_clear.strftime("%H:%m") %></center></td>
          </tr>
        </tbody>
      </table>
      <br>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #2</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><center><%= @call.user.employee_ident %></center></td>
            <td><center><%= @call.unit2_os.strftime("%H:%m") %></center></td>
            <td><center><%= @call.unit2_cl.strftime("%H:%m") %></center></td>
          </tr>
        </tbody>
      </table>
      <br>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #3</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><center><%= @call.user.employee_ident %></center></td>
            <td><center><%= @call.unit3_os.strftime("%H:%m") %></center></td>
            <td><center><%= @call.unit3_cl.strftime("%H:%m") %></center></td>
          </tr>
        </tbody>
      </table>
      <br>
      <table class="table">
        <thead>
          <tr>
            <th><center>Unit #4</center></th>
            <th><center>Time On Scene</center></th>
            <th><center>Time Clear</center></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><center><%= @call.user.employee_ident %></center></td>
            <td><center><%= @call.unit4_os.strftime("%H:%m") %></center></td>
            <td><center><%= @call.unit4_cl.strftime("%H:%m") %></center></td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>

下面我在Calls Controller中添加了供您参考,以供您参考。

Below I have added in the Calls Controller for your reference should you need it.

呼叫控制器

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

这是我在这里尝试实现的简单图表,但不知道如何进行此工作。.

这是呼叫模式表:

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

关于此问题,我在此处也发表了类似的帖子,但它更多地与选择框相关->这更多地是找出如何显示存储在DB中的Unit_1,2,3,4列(整数)中的employee_ident的过程。

I have a similar post here in regards to this question but it pertains more to the select box -> this is more of a progression to find out how to display the employee_ident that is stored in the Unit_1,2,3,4 columns (Integer) In my DB.

任何帮助都会非常有用,因为我似乎无法从其他文章或遇到此问题的用户那里找到很多东西。

Any help would be great as i cant seem to find much in terms of other articles or users having this issue.

预先感谢。

推荐答案

这是通过创建联接表来完成的。感谢所有浏览此页面的人。

This was accomplished by creating a Join Table. Thanks to all who viewed this page.

这篇关于如何引用和保存多个User_ID到一个表单中,并在索引中显示所说的ID /显示Rails 4 App的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆