大家好,我正在用 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..
这是呼叫表的架构:
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 列(整数)在我的数据库中。
任何帮助都会很棒,因为我似乎无法在其他文章或遇到此问题的用户方面找到很多帮助。
提前致谢。