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

我想要做的是从动作中发送一条闪光消息。(a ":notice => "Zip code not compatible") 但我不确定要测试什么

我试过了

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

谢谢!

附言

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

4

2 回答 2

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?

于 2013-08-06T00:28:50.460 回答
0
if @zips.empty
  redirect_to zips_path, :notice => "Zipcode is not eligible"
end
于 2013-08-06T00:22:57.513 回答