Flash[:notice] if page.blank?

167 Views Asked by At

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

4

There are 4 best solutions below

0
On

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

0
On

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
0
On

Try This

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

rediret_to page_path, notice: "Requested status is not available" if @search.present?
0
On
def search_status
  @search = User.find_by_status(params[:status])
  if [email protected]?
    flash[:notice] = 'Requested status not available'
    rediret_to '/Index'
  end
end