2016-05-30 58 views
0

重定向到city_path如果@ city.name不等於零和 「」正確使用if!= nil || != 「」 ROR

如果@ city.name等於 「」 也重定向到city_path。 如何解決它?

if (@city.name != nil || @city.name != "") 
     redirect_to city_path 
     else 
     render 'index' 
    end 
+0

嘿,你可以簡單地檢查'if(@ city.name.present?)' –

回答

3

在一個符合ternary operatorblank?方法:

@city.name.blank? ? redirect_to(city_path) : render('index') 
0

您可以使用present?方法會同時處理nilblank

if (@city.name.present?) 
     redirect_to city_path 
     else 
     render 'index' 
    end 
0

您可以像使用

if @city.name.present? 
     redirect_to city_path 
     else 
     render 'index' 
    end