受Michael Hartl教程的啓發,我開發了一個業務規則評估應用程序。 當我的用戶登錄時,他的土地的主頁上,在routes.rb中定義爲服務器啓動時Ruby on Rails搜索表單參數初始化
root to: "dashboards#home"
主頁實際顯示的前10 failling業務規則的儀表板,以及10分最近更新的。這是一個開始......並且工作正常。
然後我根據RailsCast#37添加了一個簡單搜索,以幫助快速過濾某些特定的業務規則。在發展過程中,所有工作正常,但是當我重新啓動Rails服務器,我得到這個錯誤信息對「搜索」參數:
undefined method `empty?' for nil:NilClass
評估「搜索」參數時,它只是還不存在!
下面是代碼:
- 主頁在儀表盤文件夾中定義,並通過儀表盤 推控制器。
- 儀表板控制器根據搜索功能實例化@business_rules_search
- 搜索功能在模型中實現。
主頁摘錄:
<div class="span4">
<h4>Search for Business Rules</h4>
<%= form_tag :root, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<table class="table table-striped">
<tr>
<th></th>
<th>Code</th>
<th>Name</th>
<th>Status</th>
</tr>
<% @business_rules_search.each do |business_rule| %>
<tr>
<td><%= image_tag(index_audit_tag_for(business_rule), :alt => "Quality hit") %></td>
<td class="col_narrow"><%= link_to business_rule.code, business_rule %></td>
<td><%= business_rule.name %></td>
<td><%= business_rule.status.name %></td>
</tr>
<% end %>
</table>
</div>
儀表板控制器:
class DashboardsController < ApplicationController
def home
@business_rules_index=BusinessRule.pgnd(current_playground).order("score ASC").limit(10)
@business_rules_updated=BusinessRule.pgnd(current_playground).order("updated_at DESC").limit(10)
@business_rules_search=BusinessRule.pgnd(current_playground).search(params[:search]).limit(10)
end
end
搜索功能:
def self.search(search)
if not search.empty?
where('name like ?', "%#{search}%")
else
where('1=1')
end
end
我希望你能幫助我理解了爲什麼這個參數服務器重啓後使用不起作用?
感謝您的幫助,
最好的問候,
弗雷德