Вопрос

My app implements Single Table Inheritance to address the logic of different types of user: Admin, Client, Agent with similar functionalities. All of them are written to the User table.

When rendering the users#index page I'd like for all of them to share the same partial, since I'm basically rendering the same.

#app/controllers/users_controller.rb
def index
  @users=User.all
end
<!-- app/views/users/index.html.erb -->
<div class="row">
    <h2>
        Users
    </h2>
</div>
<div class="row">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>User type</th>
                <th>Id</th>
                <th>Username</th>
            </tr>
        </thead>
        <tbody>
            <%=  render @users, as: :user, partial: 'users/user' %>
        </tbody>
    </table>
</div>
<!-- app/views/users/_user.html.erb -->
<tr>
    <th><%= user.class.name %></th>
    <th><%= user.id.to_s %></th>
    <th><%= user.username%></th>
</tr>

Whenever I try to go to the index page, I get an error of the kind Missing partial admins/admin.

I thought the line <%= render @users, as: :user, partial: 'users/user' %> would do, but it doesn't!

Это было полезно?

Решение

<%= render partial: "users/user", collection: @users, as: :user %>

Try passing an explicit collection option.

Другие советы

You could do the following instead:

 <tbody>
   <% @users.each do |user| %>
     <%= render :partial => 'users/user', :locals => {:user => user} %>
   <% end %>
 </tbody>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top