In which directory should I place my partial?

I have models Tag and Entry. Each entry can have many tags. Each tag can have many entries.

My layout has a sidebar on which a selection of tags is listed.

<%= render Tag.includes(:entries).first(10) %>

Alongside the sidebar is a list of entries. Beneath each entry is its associated tags.

<%= render partial: "tags/entry_tag", collection: entry.tags, as: :tag %>

I am using two separate partials: tags/_entry_tag.html.erb and tags/_tag.html.erb.

I suspect I should really keep the second partial in entries/ as it is related to that collection specifically.

Where should it really go? In entries/ or tags/?

Also, calling render with partial:, collection: and as: seems like it could get a bit long winded. Is there a more concise way?

As it’s a many-to-many relationship, you must have a join model, like EntryTag

I would have:

<%= render @tags %>
<%= render @entries %>


<%= render @entry_tags %>

Yes, I have the join table and model. I think that suggest though that the partial should be in the entries directory.

In response to your suggestion: The entries are rendered within their own partial. Within it, I do not have access to @entry only the local variable entry.

# .../entries/index.html.erb
<div class="entries">
  <%= render @entries %>
# .../entries/_entry.html.erb
<div class="entry flex flex-center">
  <div class="entry__info">
    <%= render partial: "description", locals: { description: entry.description } %>
    <ul class="tags margin-top-s">
      <%= render partial: "entries/entry_tag", collection: entry.tags, as: :tag %>

In entries/_entry.html.erb I would do:

<%= render entry.tags %>

That would render tags/_tag.html.erb

You can render collection: @tags, partial: 'foo/bar/tag' or just render @tags, which will by default look for app/views/tags/_tag.template-format.erb. There’s lots of ways to skin this particular feline.

In all cases, you would avoid using instance variables in your partials. You always get a “name of the partial” local variable when you render a partial, either implicitly or explicitly. If your partial file is named _entry_tag.html.erb, then within that file, you can automatically reference entry_tag as a local variable for whatever object you used to render that partial.

You can also pass in additional local variables using the locals hash syntax. There’s some subtlety to the differences between these signatures, which can get confusing at times. But basically, if you need to be explicit about where that partial can be found, all you need to do is add slashes to the partial value to disambiguate: render partial: 'shared/login' or similar.