2013-08-06 59 views
0

我有這個應用程序將顯示郵政編碼資格。我有搜索功能工作。它會在找到符合條件的郵政編碼時返回郵政編碼和郵件。數據庫沒有條目時顯示錯誤(Ruby on Rails 4.0)

但是我想在找不到條目時顯示錯誤。現在它只顯示沒有任何信息的搜索頁面。

這是我的看法

<h1> 
    Pick-up/Delivery Eligibility 
</h1> 
<h3> 
Based on U.S. Zipcode, Canadian Postal Code 
</h3> 
<p class="el"> 
    To find out if you qualify for 4over&#146;s local delivery service, 
    please enter your <nobr>U.S. 5 digit zip code, or</nobr> your 
    <nobr>Canadian 6 character Postal Code,</nobr> then click submit. 

<%= form_tag zips_path, :method => "get" do %> 
    <p><%= text_field_tag :search, params[:search] %> | <%= submit_tag "Search", :name => nil %></p> 
<% end %> 

<!-- 

<h4>Current Eligible Zip Codes</h4> 

<% @zips.each do |zip| %> 
    <p><%= link_to zip.zip, zip %>|<%= zip.state %> <%= link_to "Edit", edit_zip_path(zip) %> | <%= link_to "Delete", zip, :confirm => "Are you Sure?", :method => :delete %> </p> 
    <hr /> 
<% end %> 

<p><%= link_to "Add a New Zip", new_zip_path %></p> 

--> 

<% @zips.each do |zip| %> 
    <hr /> 
    <p> 
    Your zipcode <span style="background-color:yellow"><tt><b><%= zip.zip %></b></tt></span> is eligible for local delivery, in the following zone(s): 
    <span style="background-color:yellow"><tt><b><%= zip.state %></b></tt></span> 
    <p class=el> 
    Please fill out the form at this link: <%= link_to "Application Form", "http://tools.4over.com/feedel.pdf" %> 
    <p class="el"> 
    The following terms and conditions apply: <p class="if">We do not guarantee delivery time and date; we do our best to stay within the 
    scheduled delivery period, but in case of any emergency, extra work 
    load, or unforeseen circumstances deliveries might be postponed to the 
    following day or cancelled. 
    <p class="if">4over, Inc. will not be responsible for any 
    problem such as loss of business/customers that may be caused by late 
    delivery or NO delivery. 
    <p class="if">This is a courtesy service and we reserve 
    the right to refuse service to anyone. We reserve the right to 
    alter a specific zone/route or cancel any zone/route. 
    We do not guarantee a press time to print your jobs to match 
    with your delivery schedule. 
</p> 
<% end %> 

這是我的模型文件

class Zip < ActiveRecord::Base 
    ### 
    # Validations below 
    ### 
    validates :zip, :state, :presence => true 
    validates :zip, :length => { :minimum => 5, :maximum => 6 } 
    validates :state, :length => { :minimum => 2, :maximum => 3 } 
    validates :zip, :uniqueness => true 
    ## 
    def self.search(search) 
    if search 
     find(:all, :conditions => ['zip LIKE ?', "%#{search}%"]) 
    else 
     find(:all) 
    end 
    end 
end 

這是我在我的控制文件的操作

class ZipsController < ApplicationController 

    def index 
    @zips = Zip.search(params[:search]) 
    end 
... 
end 

我想要做的就是發送來自操作的閃光消息。 (一「:通知=>‘郵政編碼沒有資格’),但我不知道要考什麼

我試圖

if not @zips.search 
    redirect_to zips_path, :notice => "Zipcode is not eligible" 
end 

但沒有工作是否有東西,我可以?測試返回閃光燈消息

這是我的日誌

Started GET "/zips?utf8=%E2%9C%93&search=91702" for 10.7.2.31 at 2013-08-05 17:07:52 -0700 
Processing by ZipsController#index as HTML 
    Parameters: {"utf8"=>"✓", "search"=>"91702"} 
DEPRECATION WARNING: Calling #find(:all) is deprecated. Please call #all directly instead. You have also used finder options. These are also deprecated. Please build a scope instead of using finder options. (called from search at /var/www/html/localdel/app/models/zip.rb:37) 
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from search at /var/www/html/localdel/app/models/zip.rb:37) 
    Zip Load (0.9ms) SELECT "zips".* FROM "zips" WHERE (zip LIKE '%91702%') 
    Rendered zips/index.html.erb within layouts/application (0.6ms) 
Completed 200 OK in 8ms (Views: 4.6ms | ActiveRecord: 0.9ms) 


Started GET "/assets/jquery_ujs.js?body=1" for 10.7.2.31 at 2013-08-05 17:07:52 -0700 

我對Ruby和Rails的新手,所以任何幫助將是巨大的

謝謝!

PS

我使用Ruby 1.9.x的和Rails 4.x的

回答

0

嘗試更換這樣的:這個

if not @zips.search 
    redirect_to zips_path, :notice => "Zipcode is not eligible" 
end 

if @zips.search.empty? 
    redirect_to zips_path, :notice => "Zipcode is not eligible" 
end 

一個空數組([])在Ruby中解析爲「true」 。如果你在Rails中,你可以使用的另一件事是.blank?

+0

感謝您的提示!對我有用的是使用.blank? - 這給了我正在尋找的行爲。 –

0
if @zips.empty 
    redirect_to zips_path, :notice => "Zipcode is not eligible" 
end 
相關問題