我正在开发一个CAD应用程序(Rails 4,Ruby 2.2)。在电话索引页面上,我有两个面板并排。
我的目标是根据DB属性将面板排序为Active和Unassigned,该属性当前名称为Status,并且是" string"
我根本不确定如何做到这一点我试图用不太好的运气来搜索它。我使用Postgresql作为我的数据库,下面是索引页面的代码我没有修改我的控制器,但我将添加代码供参考。
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>
致电控制器:
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调用表中,如果未分配我希望它在未分配的表中。这里的任何帮助将不胜感激。
答案 0 :(得分:2)
假设您在模型Call的字段状态中有两个值1和0。在您的控制器操作中:
def index
@active_calls = Call.where(status: 1)
@inactive_calls = Call.where(status: 0)
end
然后你可以在视图中访问两个数组@active_calls和@inactive_calls,只需替换
<% @calls.each do |call| %>
到
<% @active_calls.each do |call| %>
或
<% @inactive_calls.each do |call| %>
根据您要显示它们的位置。