如何向生成的Scaffold添加其他视图并向其添加操作

时间:2015-11-22 02:12:34

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

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

如果这是一个菜鸟问题,我很抱歉,但我已经挂了......

  

背景

我有我的主索引页面,充当多视图调度窗口,目前它显示活动挂起和清除呼叫。我现在想要添加的是一个辅助页面,用于列出状态为active的呼叫,并且我最终可以添加一些搜索字段以查找以前的呼叫。

我当前的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

我已经为scaffold视图添加了一个视图,并将该视图命名为history.hmtl.erb,如下所示

enter image description here

我的routes.rb看起来像

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

然后我创建了一个按钮来访问新视图:

      <%= 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)

1 个答案:

答案 0 :(得分:1)

您可以在此处使用try

变化:

<%= @approved_calls.length %>

要:

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

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

更新

将您的history操作更改为:

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

最终更新

您的控制器操作中确实存在拼写错误。将histroy更改为history