This is a rails newb question. I have a Contents model that has a content_type attribute. I have a few different content_types that I would like to filter, passing the type through the URL like so: /contents?content_type=blog
I understand I can get the contents based on that parameter like so:
@contents = Content.where({:content_type => params[:content_type]})
But when the URL parameter is not present, it’s not getting any contents. I would prefer that when a URL param isn’t being passed, that all contents (regardless of type) would be retrieved. How do I do that?
I would define a scope, like this (inside your model)
and then you use this in your controller as follows: