Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to render STI objects without duplicating partials?

Here is the complete error rails is throwing me:

Missing partial publisher_groups/publisher_group with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: blah blah blah

In the organization show view:

<%= render @organization.groups %>

In /views/groups/_group.html.erb:

<p><%= group.name %></p>
<p><%= group.type %></p>

The relationships exist in both the group model and the org model. The error references PublisherGroup. The Group model has STI of three types that as of now don't do anything but specify type. How can I get this render to behave properly as all of the @organization.group objects are subclasses of group?

like image 624
Willis Jackson Avatar asked Dec 05 '22 15:12

Willis Jackson


1 Answers

By default trying to render an active model object tries to find a partial called table_name/class_name

In particular for an STI hierarchy it will look for a different template for each class. This is controlled by the to_partial_path instance method which returns the path to the partial

If you override that method on group

def to_partial_path
  'groups/group'
end 

Then all the subclasses will use the same partial.

like image 135
Frederick Cheung Avatar answered Jan 05 '23 00:01

Frederick Cheung