gpt4 book ai didi

ruby - Rails 脚手架索引过滤

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:26 26 4
gpt4 key购买 nike

我正在开发 CAD 应用程序(Rails 4、Ruby 2.2)。在调用索引页面上,我并排有 2 个面板。

我的目标是根据 DB 属性将面板分为事件和未分配,该属性当前名为 Status 并且是一个“字符串”

我只是完全不确定如何执行此操作,我曾尝试搜索它,但运气不佳。我使用 Postgresql 作为我的数据库,下面是索引页面的代码我没有修改我的 Controller ,但我将添加代码以供引用。

Index.html.erb代码:

  <div class="panel panel-success" id="active-pnl">
<div class="panel-heading"><center><h4>Assigned Calls: </h4></center></div>
<table class="table" id="assign-table">
<thead>
<tr>
<th><center>Call Number</center></th>
<th><center>Address</center></th>
<th><center>Responding</center></th>
<th><center>Call Type</center></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @calls.each do |call| %>
<tr>
<td><center><%= call.call_num %></center></td>
<td><center><%= call.address %></center></td>
<td><center><strong><%= call.unit_1 %></strong> | <%= call.unit_2 %> | <%= call.unit_3 %> | <%= call.unit_4 %></center></td>
<td><center><%= call.primary_type %> | <%= call.secondary_type %></center></td>
<td><center><%= link_to 'View', call, class: 'btn btn-success btn-sm', id: 'view-btn' %></center></td>
<td><center><%= link_to 'Update', edit_call_path(call), class: 'btn btn-primary btn-sm', id: 'update-btn' %></center></td>
<td><center><%= link_to 'Cancel', call, class: 'btn btn-danger btn-sm', id: 'cancel-btn', method: :delete, data: { confirm: 'Are you sure?' } %></center></td>
</tr>
<tr>

</tr>
<% end %>
</tbody>
</table>
</div>

<div class="panel panel-danger" id="unactive-pnl">
<div class="panel-heading"><center><h4>Unassigned Calls:</h4></center></div>
<table class="table" id="nonassign-table">
<thead>
<tr>
<th><center>Call Number</center></th>
<th><center>Address</center></th>
<th><center>Status</center></th>
<th><center>Call Type</center></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @calls.each do |call| %>
<tr>
<td><center><%= call.call_num %></center></td>
<td><center><%= call.address %></center></td>
<td><center><%= call.status %></center></td>
<td><center><%= call.primary_type %> | <%= call.secondary_type %></center></td>
<td><center><%= link_to 'View', call, class: 'btn btn-success btn-sm', id: 'view-btn' %></center></td>
<td><center><%= link_to 'Update', edit_call_path(call), class: 'btn btn-primary btn-sm', id: 'update-btn' %></center></td>
<td><center><%= link_to 'Cancel', call, class: 'btn btn-danger btn-sm', id: 'cancel-btn', method: :delete, data: { confirm: 'Are you sure?' } %></center></td>
</tr>
<% end %>
</tbody>
</table>
</div>



<br>
<center>
<%= link_to new_call_path, class: "btn btn-success btn-lg", id: 'newcall-btn' do %>
<i class="glyphicon glyphicon-plus"> NewCall</i>
<% end %>
</center>

调用 Controller :

class CallsController < ApplicationController
before_action :set_call, only: [:show, :edit, :update, :destroy]

# GET /calls
# GET /calls.json
def index
@calls = Call.all
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
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)
end
end

总结一下:我正在寻找能够根据调用的当前状态将调用分类到任一表中的方法。如果是 ACTIVE,我希望它出现在 Active 调用表中;如果未分配,我希望它出现在未分配的表中。这里的任何帮助将不胜感激。

最佳答案

假设您在模型调用的字段状态中有两个值 1 和 0。在您的 Controller 操作中:

def index
@active_calls = Call.where(status: 1)
@inactive_calls = Call.where(status: 0)
end

然后你可以在你的 View 中访问两个数组@active_calls和@inactive_calls,只是替换

<% @calls.each do |call| %>

<% @active_calls.each do |call| %>

<% @inactive_calls.each do |call| %>

基于您希望显示它们的位置。

关于ruby - Rails 脚手架索引过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640064/

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