我有这个应用程序将显示邮政编码资格。我有搜索功能工作。它会在找到符合条件的邮政编码时返回邮政编码和邮件。数据库没有条目时显示错误(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’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的
感谢您的提示!对我有用的是使用.blank? - 这给了我正在寻找的行为。 –