ruby on rails - Query with ransack: how to get no results by default -
ruby on rails - Query with ransack: how to get no results by default -
i page have no results default.
i accomplish same when add together .none result still after parameters set result shown. how can accomplish that?
def index @search = someclass.all @items = @search.result.page(params[:page]).per(10) end
if understand question, want index page display no records when first load view (and when filters empty). result in returning many records , user experience slow loading page.
one way address create sure execute search query only if search parameters passed action.
in controllerdef index # define search don't execute right away search # execute search query if search params exists items(params[:page],10) if search_params end private def items(page,per=10) @items ||= search.result(page).per(per) end def search @search ||= someclass.search(search_param) end def search_param params[:q] end
in view now can safely reference @search, defined in view. on other hand, @items, might or might not defined have handle both cases in view. illustration in haml:
.row = @items.nil? ? (render 'no_results') : (render 'results')
ruby-on-rails activerecord ransack
Comments
Post a Comment