I have an action with a controller that makes a list of products, pagination and some filters, for example a category (from the drop-down list), a title (from a text field), a stock (from a checkbox) This is my controller:
class ProductsController < ApplicationController
def index
@products = Product.where(active:1).where("title LIKE ?","%#{params[:title]}%")
if params[:stock]
@products=@products.where("stock = 0")
end
if params[:category]
@products=@products.where("category_id LIKE ?","#{params[:category]}")
end
@products= @products.paginate(:page => params[:page])
@categories= Category.all
end
And my model:
class Product < ActiveRecord::Base
belongs_to :category
...some validations...
end
What can I change to make my controller thinner? Thanks
source
share