Reputation: 7215
I have a class hierarchy (simplified) as follows.
Activity
SubactivityA < Activity
SubactivityB < Activity
where activity
is an instance of Activity
, subactivityA
is instance of SubactivityA
, and subactivityB
is instance of SubactivityB
.
I can use these link_to
calls:
=link_to 'Name', edit_activity_path(activity)
=link_to 'Name', edit_subactivitya_path(subactivityA)
=link_to 'Name', edit_subactivityb_path(SubactivityB)
When I'm iterating through a list of Activities, this would require me to know the subclass and specify the relevant path. Is there a way to automatically call the correct path, with something like this (which doesn't work) and have it work for all subclasses??
=link_to 'Name', edit_activity_path(activity)
Thanks!
Upvotes: 1
Views: 108
Reputation: 11628
You can use ActiveRecord's becomes to achieve the URLs you want. This is mostly useful in relation to single-table inheritance structures where you want a subclass to appear as the superclass.
Here's an example:
<% @activities.each do |activity| %>
<%= link_to 'Name', edit_activity_path(activity.becomes(Activity)) %>
<% end %>
Upvotes: 1