Filter Page Results By Column Value In Rails

Posted by Weston Ganger

My posts have a categories, I wanted to filter the index page results for specific categories. Here is how to do it

The View:

<%= link_to "Rails Results", index_path(:category_id=>"3")

The Controller:


def index
  if params[:category_id]
    @posts = Post.where(:category_id => params[:category_id])
  else
    @posts = Post.all
  end
end

Article Topic:Software Development - Rails

Date:July 03, 2014

Recommended Posts

Contact

604-309-8713
info@solidfoundationwebdev.com

Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected