Rails查询结果判断

def search
title = params[:product][:keyword]
author_name = params[:product][:author_name]
@products = Product.find(:all, :conditions => [ "title LIKE ? and author_name LIKE ? " , "%#{title}%", "%#{author_name}%" ] )

respond_to do |format|
if @products == nil || @products == ""#验证product对象不为空
flash[:notice] ="can not find the product!"
format.html{redirect_to(products_url)}
format.xml { render :xml => @products }
else if title == nil || title == "" #验证表单不为空
flash[:notice] = "keyword can not be blank !"
format.html{redirect_to(products_url)}
format.xml { render :xml => @products }

else
if @products == nil
flash[:notice] == "can not find!"
else
flash[:notice] = "find the product for you!"
format.html#{ redirect_to(search_products_url(@products)) }
format.xml { render :xml => @products }
end
end
end
end
end


但是一直都执行不了 if @products == nil || @products == ""#验证product对象不为空
以及 else
if @products == nil

其实:all的时候返回的是一个数组 数组为空 可以用@products.empty? 来判断

或者用@products.length > 0来判断

如果是一个对象 也就是object 可以用@products.nil?来判断

而且
find_by_id#没有返回就会出错
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值