I have a collection_select
in a rails form that looks like this:
<%= form.collection_select :post_id, Post.all, :id, :title, {}, { class: "mt-1 block" } %>
What I can't seem to figure out from the docs or googling, is how to pass multiple attributes from the Post to the dropdown so the user sees more than just the :title
. Something like this:
<%= form.collection_select :post_id, Post.all, :id, :title + :category, {}, { class: "mt-1 block" } %>
I can create a custom method to pass to text_method
like :title_with_category
in the Post
model like:
<%= form.collection_select :post_id, Post.all, :id, :title_with_category, {}, { class: "mt-1 block" } %>
Post.rb:
def title_with_category
self.title + " " + self.category
end
But is this the best way to do this? If so, what is the appropriate place to define this? The model? Or should this be in a helper? If it's a helper, should it be in the application helper?