简体   繁体   中英

Adding Ransack Gem with Categories

I am displaying a list of gyms, and users can view gyms by province. I would like to add search capability using the Ransack gem, but I am not sure how to go about it with my current set up. Looking for some advice.

Gyms controller

def index

    if params[:province].blank?
        @gyms=Gym.all.order("created_at desc").paginate(:page => params[:page], :per_page => 18)
        else
        @province_id = Province.find_by(name: params[:province]).id
        @gyms = Gym.where(province_id: @province_id).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)
        end

    end

thank you in advance.

Figured it out. Just replaced my existing controller code with this:

@q = Gym.ransack(params[:q]) 
@gyms = @q.result.includes(:province).page(params[:page]).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)

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