0

I am having a search form on my index page that redirects to a page that shows all statuses with the params given by search. Now I want to put in a flash notice if the page is blank because the search param doesn't fit to any of my statuses. any idea? greetz

user3079933
  • 63
  • 1
  • 9

4 Answers4

1

Try This

@search = User.find('status', params[:status])

rediret_to page_path, notice: "Requested status is not available" if @search.present?
Nitin Jain
  • 3,053
  • 2
  • 24
  • 36
HarsHarI
  • 901
  • 4
  • 11
0

This should be ok:

def index
  @statuses = Status.scope_to_retreive_statuses
  flash.now[:notice] = 'No statuses to display' if @statuses.empty?
end

More info about flash here: http://guides.rubyonrails.org/action_controller_overview.html#the-flash

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
0

Put flash notice in else condition if searched object is nil.

def search_status
  @search = User.find_by_status(params[:status])
  if @search.present?
    // Respective action
  else
    flash[:notice] = 'Requested status is not available'
    rediret_to '/Index'
  end
end
Rubyist
  • 6,486
  • 10
  • 51
  • 86
0
def search_status
  @search = User.find_by_status(params[:status])
  if !@search.present?
    flash[:notice] = 'Requested status not available'
    rediret_to '/Index'
  end
end
Bharat soni
  • 2,686
  • 18
  • 27