Theoretically, to filter the results by keywords / categories, it is good to display the logic in the same controller through the parameter. I would like to have this as:
<%= link_to 'All posts', posts_path(:published => true) %>
Then in the action of your controller / index:
def index @posts = Post.all @posts = @posts.where(:published => true) if params[:published].present? ...
To reorganize your code, I would apply a method in a model with something like:
scope :published, where(:published => true)
Then in your controller you can simply:
@posts = @posts.published if params[:published].present?
Additional information about chains / models: http://guides.rubyonrails.org/active_record_querying.html#scopes
source share