Ruby 如何引用和保存多个用户ID';s转换为单个表单并显示所述Id';在Rails 4应用程序的索引/显示页面中
嘿,我正在Rails 4 Ruby 2.2中构建一个CAD应用程序 在我的呼叫表单中,我有一个框,允许调度员选择最多4个单元发送呼叫。(布局见下文) 这个问题的简短之处在于了解如何保存和访问用户模型中的4个独立用户id,并将其存储在呼叫模型中,以便它们可以链接并显示在my Show.html.erb中,作为各自的单元1-4(如果需要所有这些单元) 这是表单:Ruby 如何引用和保存多个用户ID';s转换为单个表单并显示所述Id';在Rails 4应用程序的索引/显示页面中,ruby,html,css,postgresql,ruby-on-rails-4,Ruby,Html,Css,Postgresql,Ruby On Rails 4,嘿,我正在Rails 4 Ruby 2.2中构建一个CAD应用程序 在我的呼叫表单中,我有一个框,允许调度员选择最多4个单元发送呼叫。(布局见下文) 这个问题的简短之处在于了解如何保存和访问用户模型中的4个独立用户id,并将其存储在呼叫模型中,以便它们可以链接并显示在my Show.html.erb中,作为各自的单元1-4(如果需要所有这些单元) 这是表单: <%= form_for(@call) do |f| %> <div class="panel panel-su
<%= 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 %>
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
这里的问题是没有引用所有4个单元的用户id。当我尝试在显示页面上连接用户id“1”以显示每个员工的识别号时,仅显示该id。为了显示这一点,我在Show.html.erb中使用了以下内容
<td><center><%= @call.user.employee_ident %></center></td>
关于这个问题,我在这里有一个类似的帖子,但它更多地涉及到选择框->这是一个了解如何在我的数据库中显示存储在Unit_1,2,3,4列(整数)中的员工标识的过程
任何帮助都将是伟大的,因为我似乎找不到太多关于其他文章或用户有这个问题
提前谢谢 这是通过创建联接表来实现的。感谢所有浏览此页面的人 你能粘贴你的
User
和Call
模型吗?我建议你应该像Call有很多用户那样进行关联,我已经添加了控制器,但是关系已经设置好了。。我需要知道如何引用多个user\u id,因为目前我的calls模型中只有user\u id列。。我可以添加几个user\u id列吗?不,在用户模型中添加call\u id列
<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
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