0

Redirect to city_path if @city.name not equal nil and ""

If @city.name equal "" also redirect to city_path. How to fix it?

if (@city.name != nil || @city.name != "")
      redirect_to city_path
      else
      render 'index'
    end
Stiven Frams
  • 117
  • 1
  • 2
  • 7

3 Answers3

3

In one line with ternary operator and blank? method:

@city.name.blank? ? redirect_to(city_path) : render('index')
Community
  • 1
  • 1
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
0

You can use present? method it will handle both nil and blank values

if (@city.name.present?)
      redirect_to city_path
      else
      render 'index'
    end
Vishal JAIN
  • 1,940
  • 1
  • 11
  • 14
0

You can use like

 if @city.name.present?
      redirect_to city_path
      else
      render 'index'
    end
Kaushlendra Tomar
  • 1,410
  • 10
  • 16