ruby-on-rails - 如何向生成的脚手架添加额外的 View 并向其添加操作

标签 ruby-on-rails ruby postgresql ruby-on-rails-4 ruby-on-rails-3.2

嘿,我正在用 Rails 4 Ruby 2 构建一个 CAD 应用程序

如果这是一个菜鸟问题,我深表歉意,但我卡在这里了..

BACKGROUND

我有我的主索引页面,充当多 View 调度窗口,目前它显示事件的待处理和已清除的调用。我现在要添加的是一个辅助页面,用于列出状态为事件的调用,我最终可以添加一些搜索字段来查找以前的调用。

我当前的 calls_controller.rb 索引如下所示:

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'}
    @clear_calls = @calls.select{|x| x.status == 'CLEAR'}
    @approved_calls = @calls.select{|x| x.status == "APPROVED"}
  end

**I HAVE THEN ADDED**

  def histroy
    @calls = Call.all
    @approved_calls = @calls.select{|x| x.status == "APPROVED"}
  end

我在脚手架 View 中添加了一个 View ,并将该 View 命名为 history.hmtl.erb,如下所示:

enter image description here

我的 routes.rb 看起来像

  resources :calls do
    collection do
      get 'history'
    end
  end

然后我创建了访问新 View 的按钮:

      <%= link_to history_calls_path, class: "btn btn-primary btn-nav", id: 'home-btn' do %>
        <i class="fa fa-search"> Call History</i>
      <% end %>                     

当我点击按钮访问页面时出现以下错误:

NoMethodError in Calls#history
undefined method `length' for nil:NilClass --> There are already calls with that status but it should return 0 if there are no approved calls

Extracted source (around line #2):
1
2
3
4
5
6

  <div class="panel panel-warning" id="clr-calls-pnl">
  <div class="panel-heading"><center><h4>Call History -- TOTAL CLEARED CALLS <span class="badge"><%= @approved_calls.length %></span></h4></center></div>
    <table class="table" id="approve-table">
      <thead>
        <tr>
          <th><center>Call Number</center></th>

这里的任何帮助将不胜感激,因为我仍然是一个菜鸟,但以前从未做过这种事情。

提前致谢。

编辑#1:

我把它改成了 .try(:lenght)

现在我明白了:

NoMethodError in Calls#history
Showing /Users/TaurenLTD1/Desktop/TaurenLabs/PatrolProCAD/PatProCadApp/app/views/calls/history.html.erb where line #31 raised:
undefined method `each' for nil:NilClass

Extracted source (around line #31):
29
30
31
32
33
34


      <tbody>
        <% @approved_calls.each do |call| %>
        <tr>
          <td><center><%= call.call_number %></center></td>
          <td><center><%= call.site_id %></center></td>

编辑#2:

This is what the rails log is showing when I Open the History Page:

Started GET "/calls/history" for ::1 at 2015-11-21 19:46:55 -0700
Processing by CallsController#history as HTML
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
  Rendered calls/history.html.erb within layouts/application (3.7ms)
Completed 200 OK in 131ms (Views: 129.9ms | ActiveRecord: 0.3ms)

最佳答案

您可以使用 try在这里。

改变:

<%= @approved_calls.length %>

收件人:

<%= @approved_calls.try(:length) %>

由于某种原因,您的 @approved_callsnil,这就是您出现该错误的原因。以上内容应该可以防止您收到该错误。但是,更好的方法是查看为什么 @approved_calls 获得 nil 值并修复该部分并确保填充 @approved_calls 实例变量.

更新

将您的历史记录 操作更改为:

  def history
    @approved_calls = Call.where(status: 'APPROVED')
  end

最终更新

您的 Controller 操作实际上有错字。将 histroy 更改为 history

关于ruby-on-rails - 如何向生成的脚手架添加额外的 View 并向其添加操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850906/

相关文章:

ruby-on-rails - 如何让 ActionController::Live streaming 与 Thin 一起工作?

ruby-on-rails - 未知编码名称 - CP720 (ArgumentError) - 启动服务器时 ruby​​ on rails 错误

ruby-on-rails - 有没有Rails 方法来获取ActiveRecord::Relation 的所有关联对象?

php 应用程序/控制台学说 :database:import returns OutOfMemoryException

ruby-on-rails - rails : How can I log all requests which take more than 4s to execute?

ruby-on-rails - 测试驱动开发?精神 split 症?我糊涂了!我应该使用什么进行测试,为什么?

ruby-on-rails - Ruby on Rails - ElasticSearch 结果窗口太大

sql - MySQL 是否可以通过一次查询返回多个结果集?

python - 如何在 Postgresql hstore 中存储 python 字典

sql - 选择更新等待网络操作?