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#没有返回就会出错