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

2024-04-29

大家好,我正在用 Rails 4 Ruby 2.2 构建一个 CAD 应用程序;

在我的“呼叫”表单中,我有一个框,允许调度员选择最多 4 个设备发送到呼叫。 (布局见下文)。

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

这是形式:

<%= 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 %>

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

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

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

使用当前的collection_select,它更新了Postgresql数据库中的用户编号,我已通过Rails控制台验证了该用户编号(见下文)

Rails 控制台输出:

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

这里的问题是没有引用所有 4 个单元的 user_id。当我尝试在显示页面上连接它以显示每个员工身份号码时,仅显示 user_id: "1" 。为了显示它,我在 Show.html.erb 中使用以下内容

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

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

这是我的显示页面:

<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>

下面我添加了呼叫控制器供您需要时参考。

呼叫控制器

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

Here is a simple diagram of what i am trying to acheive here but have no idea how to make this work.. enter image description here

这是呼叫表的架构:

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

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

任何帮助都会很棒,因为我似乎无法在其他文章或遇到此问题的用户方面找到很多帮助。

提前致谢。


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何引用多个 User_ID 并将其保存到单个表单中,并在索引/显示 Rails 4 应用程序页面中显示所述 ID 的相关文章

  • 有没有人有 Ruby 和 Rake 的 Notepad++ 函数列表插件的解析规则

    我使用 Notepad 编辑 rake 文件 并且希望能够使用函数列表插件 我无法在线找到任何解析规则 并且 语言解析规则 对话框没有非常清晰的记录 我正在将方法解析到以下列表中 但还想显示任务 Function Begin t def t
  • JavaFX 中的 -fx-background-radius 和 -fx-background-insets

    我正在使用 JavaFX 我想自定义一个按钮 我看到了几个可以设计的特征 其中我发现了两个我不认识的人 button fx padding 5 22 5 22 fx border color 121212 fx border width 2
  • 使用过渡+不透明度更改+溢出隐藏时出现问题

    如果您看到我共享的代码示例 您可以看到覆盖层超出了框的范围 我将问题追溯到transition属性 我想删除div之外的内容 溢出没有按预期工作 删除transition有效 但如果可能的话我想保留它 任何帮助表示赞赏 代码笔链接 http
  • Twitter Bootstrap - 多图像(缩略图)轮播 - 一次移动一个缩略图

    我正在尝试 Twitter bootstrap 3 我对 HTML CSS 和 Javascript 还很陌生 我有一个我创建的轮播 它的代码如下所示 div class container div class carousel slide
  • Postgres 平均值计算忽略 null

    这是我的 postgres 表 name revenue John 100 Will 100 Tom 100 Susan 100 Ben 5 rows 在这里 当我计算平均收入时 它返回 100 这显然不是这种情况 而总和 计数 即 400
  • 如何在 Bootstrap 4 中垂直对齐?

    我试图在 Bootstrap 4 4 0 0 alpha 6 中将我的大屏幕的内容垂直对齐在中心 在 Mac 桌面上的 Chrome 和 Safari 中 这种情况发生得很好 但在我的 iOS 设备上则不然 文本仍然与顶部对齐 我强制大屏幕
  • Chrome 中的错误或 CSS 错误? (隐藏可见性的锚点)

    在任何 HTML 中测试这个简单的行 a href anything span insible text here span a 您可以直接从这里测试 http jsfiddle net wqS3E http jsfiddle net wq
  • css 字体 twitter 像关闭按钮一样,我错过了什么?

    twitter 有一个关闭按钮 它是单个字符 x 我需要类似的东西 但是在我检查之后 span x span 我按照 firebug 告诉我的那样创建 css 规则 close button font family Tahoma Arial
  • 如何向 Time.now 添加两周?

    如何在 Ruby 中向当前 Time now 添加两周 我有一个使用 DataMapper 的小型 Sinatra 项目 在保存之前 我有一个字段填充了当前时间加上两周 但未按需要工作 任何帮助是极大的赞赏 我收到以下错误 NoMethod
  • postgresql 中的锁定表

    我有一个名为 games 其中包含一个名为 title 该列是唯一的 数据库中使用PostgreSQL 我有一个用户输入表单 允许他插入新的 game in games 桌子 插入新游戏的功能会检查之前输入的游戏是否存在 game 与相同的
  • Rails Active Admin css 与 Twitter Bootstrap css 冲突

    我对 Rails 资产管道有点陌生 所以我可能做错了什么 我正在尝试为我的后端使用 Active Admin 为我的前端应用程序使用 twitter bootstrap css 我将 bootstrap css 添加到 应用程序 资产 样式
  • 数据表“footerCallback”函数未在页脚中显示结果

    我尝试获取每列的总和并将结果显示在页脚中 我在用着 页脚回调 https datatables net reference option footerCallbackDatatables提供的功能 但是它在页脚中没有显示任何内容 数据表解释
  • (CSS) 倾斜 img 框架而不扭曲图像

    我正在制作一个包含许多倾斜元素的网站 如下所示 这还不错 CSS 转换可能会扭曲它 但是这个怎么样 图像没有扭曲 只是框架以倾斜的方式裁剪 最简单 最好的方法是什么 I think this http codepen io antiblan
  • 清理 html 字符串中的所有脚本

    HTML5 剪贴板很棒 但我正在寻找一种使其安全的方法 用户正在将文本 html 粘贴到我的网页中 这允许他们粘贴图像 表格等 我正在寻找一种方法 在将粘贴的内容添加到页面之前删除所有脚本 我需要删除
  • CSS:如何在模糊的背景上剪切文本?

    我想重新创建以下样式 我想出了以下内容 问题是剪切不会影响模糊滤镜 我不知道如何解决它 这是我的 HTML 代码 glass width 40 height 100 position absolute background rgba 255
  • CSS :hover 影响所有子 div

    我里面有一个父 div 和多个子 div 我希望这样 如果您将鼠标悬停在父 div 上 它会以不同的方式影响所有子 div 的悬停状态 即 一个 div 的文本带有下划线 另一个 div 的文本会更改颜色 并且保存图像的 div 使图像稍微

  • 有多少像素? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 一个有多少像素 br 标签添加 我知道 br 高度可以通过CSS控制line height财产 不同浏览器的值是否相同 它会添加与浏览
  • div 之间的额外空间从何而来?

    http www lethalmonk6 byethost24 com index html http www lethalmonk6 byethost24 com index html 如果您使用 firebug 检查 项目链接 div
  • 简单的颜色变化

    我正在创建一个用户界面 用户可以在其中更改页面的颜色值 我想要的是获取分配给其背景颜色的值并将其变亮一定程度 我只是想获得一条亮点线 而不必每次都制作新图像 示例 用户将背景颜色设置为 ECECEC 现在我希望某个元素边框变成 F4F4F4
  • tr 元素周围的边框不显示?

    Chrome Firefox 似乎不渲染边框tr 但如果选择器是 它会渲染边框table tr td 如何在 tr 上设置边框 我的尝试 不起作用 table tr border 1px solid black table tbody tr

随机推荐