首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails Scaffold指数滤波

Rails Scaffold指数滤波
EN

Stack Overflow用户
提问于 2015-11-10 21:36:06
回答 1查看 145关注 0票数 0

我正在开发一个CAD应用程序(Rails 4,Ruby2.2)。在呼叫索引页上,我有两个并排的面板。

我的目标是根据DB属性将面板排序为活动的和未分配的,该属性目前是名称状态,是一个“字符串”。

我只是不知道该怎么做我使用Postgresql作为我的DB和下面是索引页的代码,我没有修改我的控制器,但我将添加代码以供参考。

Index.html.erb代码:

代码语言:javascript
复制
  <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>

调用控制器:

代码语言:javascript
复制
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

因此,概述:我正在寻找的东西,将排序的调用到任何一个表,根据其当前的状态。如果是活动的,我希望它在活动调用表中,如果没有分配,我希望它在未分配的表中。这里的任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-10 21:50:09

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

代码语言:javascript
复制
def index
    @active_calls = Call.where(status: 1)
    @inactive_calls = Call.where(status: 0)
end

然后可以访问视图中的两个数组@active_calls和@inactive_calls,只需替换

代码语言:javascript
复制
<% @calls.each do |call| %>

代码语言:javascript
复制
<% @active_calls.each do |call| %>

代码语言:javascript
复制
<% @inactive_calls.each do |call| %>

基于您想要显示它们的位置。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33640064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档