Rails查詢結果判斷

2021-08-30 01:25:24 字數 1101 閱讀 5332

def search

title = params[:product][:keyword]

author_name = params[:product][:author_name]

@products = product.find(:all, :conditions => [ "title like ? and author_name like ? " , "%#%", "%#%" ] )

respond_to do |format|

if @products == nil || @products == ""#驗證product物件不為空

flash[:notice] ="can not find the product!"

format.html

format.xml

else if title == nil || title == "" #驗證表單不為空

flash[:notice] = "keyword can not be blank !"

format.html

format.xml

else

if @products == nil

flash[:notice] == "can not find!"

else

flash[:notice] = "find the product for you!"

format.html#

format.xml

endend

endend

end

但是一直都執行不了 if @products == nil || @products == ""#驗證product物件不為空

以及 else

if @products == nil

其實:all的時候返回的是乙個陣列 陣列為空 可以用@products.empty? 來判斷

或者用@products.length > 0來判斷

如果是乙個物件 也就是object 可以用@products.nil?來判斷

而且

find_by_id#沒有返回就會出錯

Rails 根據 user agent 判斷請求端

要根據請求端的不同而返回不同的頁面,首先就得判斷出請求端是pc還是mobile。在http請求時,user agent這個欄位會記錄使用者所用的裝置和瀏覽器。所以就得對user agent進行解析,來獲取是什麼裝置。1 在rails專案中,要用到useragent 這個gem 包 輸入終端命令 ge...

查詢結果分頁

以下摘自msdn 查詢結果分頁 dataadapter 提供了通過 fill 方法的過載來僅返回一頁資料的功能。但是,對於大量的查詢結果,它可能並不是首選的分頁方法,因為 dataadapter 雖然僅使用所請求的記錄來填充目標 datatable 或 dataset,但仍會使用返回整個查詢的資源。...

mysql得到查詢結果的同時統計查詢結果的數目

做一些資料庫查詢,不僅希望得到要查詢的結果,還希望方便地統計一下查詢結果中有多少條記錄。我通常的做法是 q select from fromtable where where limit start,pagesize r mysql query q q select count from fromt...