DB没有条目时显示错误(Ruby on Rails 4.0)

时间:2013-08-06 00:18:19

标签: ruby-on-rails ruby

我有这个应用程序将显示邮政编码资格。我的搜索功能正常运行。它会在找到符合条件的邮政编码时返回邮政编码和邮件。

但是我想在找不到条目时显示错误。现在它只显示没有信息的相同搜索页面。

这是我的观点

<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

我想要做的是从动作发送一条flash消息。 (a“:notice =&gt;”邮政编码不符合条件“)但我不确定要测试什么

我试过

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

但那没用。我可以测试一些东西来返回flash消息吗?

这是我的日志

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的新手;所以任何帮助都会很棒!

谢谢!

P.S

我正在使用Ruby 1.9.x和Rails 4.x

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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?