I’ve got a partial view that I can use to show several types of models. The only difference I have between objects is a subtitle otherwise the partial is unchanged between objects. I’ve gone with:
if object.is_a? SomethingA
elsif object.is_a? SomethingB
However, I’ve read that is_a? is something to be avoided. How should I be handling this snippet of view logic?
One way: define
to_partial_path on each object. Have a different partial for each object type and use those to hold the differences.
So your view would look like this:
<%= render @object %>
Each object would define
to_partial_path (which Rails uses to find the partial to render in our call above.
For further discussion and examples, check out Ruby Science; specifically the Polymorphic Partials section.
Thanks @benorenstein, what would be the strategy if you had already used the
I love the concept of to
to_partial_path and it seems like a powerful tool. Our application is showing objects in lots of different ways throughout the application and I wonder if there is a way to have several
to_partial_path options? Maybe:
def to_partial_path(style = :default)
# default path
# super awesome path
# raise UnkownStyleError
Can you see any reason this would be a bad idea? Can you even pass the argument through?
That would break the normal api for
to_partial_path (which normally takes no argument) and so I would recommend against it. And as you guess, I don’t think you can even pass an argument through.
Instead, you can make a new helper method that builds on top of
So if you have an Episode model, it would make it so that the helper would return
The helper can be used like this:
render title_partial_path(episode), episode: episode
Thanks @cpytel. That will work great!