简体   繁体   中英

Displaying a message if no results were found in postgresql full text search

I am using a postgresql full text search. I have implemented everything successfully. I am just having small trouble with a small if and else statement. I want to display a message, "no results found" if no queries matched. The model, view and controller codes are given below respectively.

Model
    include PgSearch
      pg_search_scope :search_by_scoreboard, 
                        :against => [:name_of_scoreboard, :name_of_organization, :name_of_activity],
                        :using => {
                            :tsearch => {
                                tsvector_column: "tsv",
                                :prefix => true,
                                :dictionary => "english",
                                :any_word => true
                            }

                        }

View

    <div id="search">
    <%= form_tag search_path, :method => 'get' do %>
      <%= text_field_tag :search, params[:search], placeholder: "search scorecliq!" %>
      <div id="searchbutton">
      <%= submit_tag "Search", :name => nil %>
      </div>
    <% end %>
    <div>

    <% if params[:search].present? %>

    <% @scoreboard_search.each do |scoreboard_search| %>
        <%= div_for scoreboard_search, :class => "inner-disp" do %> 
            Scoreboard: <%= link_to "#{scoreboard_search.name_of_scoreboard}", scoreboard_path(scoreboard_search.id) %><br>
            Organization: <%= scoreboard_search.name_of_organization %><br>
            Activity: <%= scoreboard_search.name_of_activity %><br>
            state: <%= scoreboard_search.states %><br>
            Admin: <%= link_to "#{image_tag scoreboard_search.user.picture}", user_path(scoreboard_search.user.id)  %>
            picture: <%# image_tag scoreboard_search.user.picture %>
        <% end %>
    <% end %>

    <%= will_paginate @scoreboard_search, :page_links => true %>


    <% end %>

controller

    def index
        @scoreboard_search = Scoreboard.search_by_scoreboard(params[:search]).paginate(:page => params[:page], :per_page => 5)
    end 

If it was a simple query, I could write the if and else statement in the models. However, I not sure how to do that with postgresql full text. I tried this in the controller as well but it didn't work. I have tried if params[:search].present but thats not the right approach. It seems simple enough but I have been struggling with this for some time now. Any help would be greatly appreciated, Thanks!!

You can put a condition in your view to check @scoreboard_search contains result or not.

 <% if params[:search].present? %>
       <% if @scoreboard_search.present? %>
            <% @scoreboard_search.each do |scoreboard_search| %>
                <%= div_for scoreboard_search, :class => "inner-disp" do %> 
                    Scoreboard: <%= link_to "#{scoreboard_search.name_of_scoreboard}", scoreboard_path(scoreboard_search.id) %><br>
                    Organization: <%= scoreboard_search.name_of_organization %><br>
                    Activity: <%= scoreboard_search.name_of_activity %><br>
                    state: <%= scoreboard_search.states %><br>
                    Admin: <%= link_to "#{image_tag scoreboard_search.user.picture}", user_path(scoreboard_search.user.id)  %>
                    picture: <%# image_tag scoreboard_search.user.picture %>
                <% end %>
            <% end %>

            <%= will_paginate @scoreboard_search, :page_links => true %>

        <% else %>
          No result found
        <% end %>
    <% end %>

or check in controller if you want to show a message through flash. controller

   def index
        @scoreboard_search = Scoreboard.search_by_scoreboard(params[:search]).paginate(:page => params[:page], :per_page => 5)

        or you can show a flash message 
        flash[:notice] = "No result found" if @scoreboard_search.nil? || @scoreboard_search.blank?
    end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM