如何向生成的脚手架添加额外的视图并向其添加操作
How to add an additional View to generated Scaffold and adding an action to it
嘿,我正在 Rails 4 Ruby 2
中构建 CAD 应用程序
如果这是一个菜鸟问题,我深表歉意,但我挂在这里了..
BACKGROUND
我有我的主索引页面,充当多视图调度 window,目前它显示活动的待处理和已清除的呼叫。我现在要添加的是一个辅助页面,用于列出状态为活动的呼叫,我最终可以添加一些搜索字段来查找以前的呼叫。
我当前的 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
我已经在脚手架视图中添加了一个视图,并将该视图命名为 history.hmtl.erb,如下所示:
我的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" = 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_calls
是 nil
,这就是您出现该错误的原因。以上内容应该可以防止您收到该错误。但是,更好的方法是查看为什么 @approved_calls
获得 nil
值并修复该部分并确保填充 @approved_calls
实例变量。
更新
将您的 history
操作更改为:
def history
@approved_calls = Call.where(status: 'APPROVED')
end
最终更新
您的控制器操作实际上有错字。将 histroy
更改为 history
.
嘿,我正在 Rails 4 Ruby 2
中构建 CAD 应用程序如果这是一个菜鸟问题,我深表歉意,但我挂在这里了..
BACKGROUND
我有我的主索引页面,充当多视图调度 window,目前它显示活动的待处理和已清除的呼叫。我现在要添加的是一个辅助页面,用于列出状态为活动的呼叫,我最终可以添加一些搜索字段来查找以前的呼叫。
我当前的 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
我已经在脚手架视图中添加了一个视图,并将该视图命名为 history.hmtl.erb,如下所示:
我的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" = 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_calls
是 nil
,这就是您出现该错误的原因。以上内容应该可以防止您收到该错误。但是,更好的方法是查看为什么 @approved_calls
获得 nil
值并修复该部分并确保填充 @approved_calls
实例变量。
更新
将您的 history
操作更改为:
def history
@approved_calls = Call.where(status: 'APPROVED')
end
最终更新
您的控制器操作实际上有错字。将 histroy
更改为 history
.