Saturday, 15 February 2014

search - how to filter products details using in rails 4 -



search - how to filter products details using in rails 4 -

i developing application there 2 models: mobile brand , mobile product. every brand has_many :products.

in index page showing products , left side want show ranges other shopping card, example:

search price

10000-15000 15000-20000 20000-25000 prices > 25,000

search ram

256 mb 512 mb

search cam

13 mp 8 mp 5 mp

search screen

less 3 inches 3.0 inch - 4.0 inch 4.1 inch - 4.9 inch

like want show when click link show details.

so please help me how write in product controller, need create different action in controller , different view page. give me few code thought how it.

you can utilize scopes solve problem. show how filter cost range , believe same process can used other filters.

have in product model

class product < activerecord::base ## other code scope :price_min, lambda{|min| where(['price >= ?', min])} scope :price_max, lambda{|max| where(['price <= ?', max])} ## other code end

in products_controller.rb have this:

class productscontroller < applicationcontroller has_scope :price_min has_scope :price_max def index @products = product.all end ## other code end

in app/views/products/index.html.erb have this:

<%= link_to '10000-15000', @products_path, price_min: 10000, price_max: 15000 %> <%= link_to '15000-20000', @products_path, price_min: 15000, price_max: 20000 %> <%= link_to '20000-25000', @products_path, price_min: 20000, price_max: 25000 %>

edit: ensure have in gemfile

gem 'has_scope'

and run bundle command.

ruby-on-rails search

No comments:

Post a Comment