gpt4 book ai didi

ruby - 如何使用从同一模型中提取的多个 collection_select 框?

转载 作者:行者123 更新时间:2023-11-29 13:26:05 24 4
gpt4 key购买 nike

我正在使用 Rails 4 和 Ruby 2.2 构建一个 CAD 应用程序。

我遇到的问题如下:

我正在尝试使用 collection_select(动态选择框)将用户 employee_ident 号码拉入多个“响应单位”行。有些会被使用,有些不会总是被使用。

我在我的索引、表单和编辑页面中列出了 4 行都比较准确。他们都引用了 user_id ..

我发现的问题是,当我为 unit_1 选择单元时,单元 2、3 和 4 都拉过我放在第一个框中的用户的 user_id。

这是整个相关面板的 HTML 和 ERB:

<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><%= collection_select(:call, :unit_1, User.all, :id, :employee_ident, {}, { :multiple => false } ) %></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><%= collection_select(:call, :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><%= collection_select(:call, :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><%= collection_select(:call, :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>

我把 unit_1 放回 unit_4 这样你就可以看到我想做什么,但最初我有

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

但是对于 unit_1 到 4,即使我将它设置为一个整数,当我尝试像这样连接它时它仍然不会拉:

<%= @call.user.employee_ident %>

我当前的 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

显示页面代码:

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

Rails 控制台输出:

2.2.0 :013 > c = Call.last
Call Load (0.4ms) SELECT "calls".* FROM "calls" ORDER BY "calls"."id" DESC LIMIT 1
=> #<Call id: 3, call_time: "2015-11-11 09:31:00", status: "ACTIVE", primary_type: "EXPLOSION", secondary_type: "N/A", address: nil, unit_1: "1", unit_2: "2", unit_3: "3", unit_4: "1", call_details: "Reports of explosion at location, proceed with cau...", unit_on_scene: "2000-01-01 09:31:00", unit_clear: "2000-01-01 09:31:00", site_id: 2, created_at: "2015-11-11 09:32:44", updated_at: "2015-11-11 23:08:42", user_id: 1, unit2_os: "2000-01-01 09:57:00", unit2_cl: "2000-01-01 09:57:00", unit3_os: "2000-01-01 09:57:00", unit3_cl: "2000-01-01 09:57:00", unit4_os: "2000-01-01 09:57:00", unit4_cl: "2000-01-01 09:57:00", call_number: 1001>

如您所见,它在 unit_1、2、3 和 4 框中添加了 employee_idents 4114、4112、4113 的 user_id,但输出的只是来自 user_id 列的数据DB.. 无论如何我可以在单个 user_id 记录中存储多个 user_id 吗?

这将给我 4114 的 employee_ident 清楚地显示在显示、索引页面等中。

任何帮助将不胜感激,我在这里被难住了..

最佳答案

Controller 看起来不错 - 所以可能是表单设置的方式。

如果您从使用独立的 collection_select 更改为调用表单对象时会发生什么 (f) 所以而不是:

<%= collection_select(:call, :unit_1, User.all, :id, :employee_ident, {}, { :multiple => false } ) %>

尝试

<%= f.collection_select(:unit_1, User.all, :id, :employee_ident, {}, { :multiple => false } ) %>

关于ruby - 如何使用从同一模型中提取的多个 collection_select 框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33647582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com