2014-06-11 65 views
1

受Michael Hartl教程的啓發,我開發了一個業務規則評估應用程序。 當我的用戶登錄時,他的土地的主頁上,在routes.rb中定義爲服務器啓動時Ruby on Rails搜索表單參數初始化

root to: "dashboards#home" 

主頁實際顯示的前10 failling業務規則的儀表板,以及10分最近更新的。這是一個開始......並且工作正常。

然後我根據RailsCast#37添加了一個簡單搜索,以幫助快速過濾某些特定的業務規則。在發展過程中,所有工作正常,但是當我重新啓動Rails服務器,我得到這個錯誤信息對「搜索」參數:

undefined method `empty?' for nil:NilClass 

評估「搜索」參數時,它只是還不存在!

下面是代碼:

  1. 主頁在儀表盤文件夾中定義,並通過儀表盤 推控制器。
  2. 儀表板控制器根據搜索功能實例化@business_rules_search
  3. 搜索功能在模型中實現。

主頁摘錄:

 <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 

我希望你能幫助我理解了爲什麼這個參數服務器重啓後使用不起作用?

感謝您的幫助,

最好的問候,

弗雷德

回答

1

可以使用.present?方法:

def self.search(search) 
    if search.present? 
    where('name like ?', "%#{search}%") 
    else 
    where('TRUE') 
    end 
end 

.present?是非常有用的,它結合了.nil?.empty?

nil.present? # => false 
"".present? # => false 
[].present? # => false 
"hey".present? # => true 
[1].presence? # => true 

.present?.blank?正好相反:

# File activesupport/lib/active_support/core_ext/object/blank.rb, line 18 
def present? 
    !blank? 
end 

# File activesupport/lib/active_support/core_ext/object/blank.rb, line 13 
def blank? 
    respond_to?(:empty?) ? empty? : !self 
end 

所以:

nil.blank? # => true 
"".blank? # => true 
[].blank? # => true 
"hey".blank? # => false 
[1].blank? # => false 
1

使用present?而不是empty?

def self.search(search) 
    if search.present? 
    where('name like ?', "%#{search}%") 
    else 
    where('1=1') 
    end 
end 

由於empty?方法沒有被定義爲NilClass,當search參數爲nil時,您肯定會收到錯誤undefined method 'empty?' for nil:NilClassempty?方法可用於字符串,數組和哈希,但不能用於nil對象。

searchnil時,search.present?返回false所以其他條件將被執行。

當您重新啓動服務器並訪問應用程序的根頁面時,params[:search]nil,因爲您尚未搜索任何內容並直接呈現根頁面。當你實際上搜索業務規則通過在文本字段中輸入搜索字符串params[:search]不會是nil

相關問題