【问题标题】:Rails Scaffold Index FilteringRails 脚手架索引过滤
【发布时间】:2015-11-10 21:36:06
【问题描述】:

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

我的目标是根据数据库属性将面板分类为活动和未分配,当前名称为状态并且是“字符串”

我根本不知道该怎么做我试图搜索它,但运气不佳。我使用 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 calls 表中,如果未分配,我希望它出现在未分配的表中。这里的任何帮助将不胜感激。

【问题讨论】:

    标签: ruby html css postgresql ruby-on-rails-4


    【解决方案1】:

    假设您在模型调用的字段状态中有两个值 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| %>
    

    根据您想要显示它们的位置。

    【讨论】:

    • 那么我应该将列更改为整数值吗?还是在发送到数据库时仅使用选择字段并将值设置为 1 和 0?
    • 不,您不需要更改数据库中的列。如果您的列是字符串,也可以。只要给那里的价值。例如,如果您的值是“活动”和“非活动”,只需将其更改为 @active_calls = Call.where(status: "active")
    • 非常感谢您的快速响应,效果很好!!!非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2019-07-21
    • 1970-01-01
    • 2011-08-16
    • 2015-12-29
    相关资源
    最近更新 更多