I have a lot pages in my app. And for every page I need to create a new variable and describe method. And if I will need to change something, I will have to change on every page. So the idea is to create universal method in application_controller.rb.
For example, I have a lot of categories in my Posts and to target some category for page I did: @posts_for_interesting = Post.where(interesting: true), other category, for example: @posts_for_photos = Post.where(photos: true).
And the application_controller.rb, have to look something like that:
def posts_for_all_pages(category)
@posts = Posts.where(category: true)
end
And, for example, photos_controller.rb must look like that:
posts_for_all_pages(photos)
And how can I pass this photos to Post.where(category: true)?
Right here in your original code:
def posts_for_all_pages(category)
@posts = Posts.where(category: true)
end
The category in Posts.where(category: true) will not a variable, it will be a hard coded symbol :category, so it won't work. Instead, write this:
def posts_for_all_pages(category)
@posts = Posts.where(category => true)
end
a small change, but definitely has a different meaning.
Then when you call the method, you pass a symbol to it:
posts_for_all_pages(:photos)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With