Rails

Filter Page Results By Column Value In Rails

Posted by Weston Ganger on July 03, 2014

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

Posted in Rails and Tagged with rails 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts