I’m trying to create an index page where all assets are displayed including a link to the user who they are assigned to, however when I use
def index
@assets = Asset.paginate(page: params[:page])
@user = User.find(params[:id])
end
It returns – couldn’t find User without an ID.
index.html.erb – Assets
<%= provide(:title, 'Assets') %>
<h1>All assets</h1>
<%= will_paginate @assets %>
<ol class="assets">
<%= render @assets %>
</ol>
<%= will_paginate @assets %>
_asset.html.erb partial
<li>
<span class="id">JTC<%= asset.id %></span>
<span class="serial"><%= asset.serial_no %></span>
<span class="status">Status: <%= asset.status %></span>
<span class="type">Type: <%= asset.asset_type %><br/></span>
<span class="description">Description: <%= asset.asset_description %><br /></span>
<span class="comment"><em><%= asset.comment %></em></span>
<span class="timestamp">
<br />Added <%= time_ago_in_words(asset.created_at) %> ago. <br /> Assigned to: <%= link_to
@user.name %>
<br />Date Purchased: <%= asset.date_purchased %>
</span>
</li>
@user = User.find(params[:id])is expecting a URL along the lines of/assets/40, where 40 is the id (and even then that wouldn’t make sense, since the 40 should be the id of the asset, not the user). When you just access/assets, there is no id parameter, so it tries to basically doUser.find(nil), which gives you that error.Assuming assets belong to users, you should just get rid of the
@user = User.find(params[:id])line in your controller, and change your partial to useasset.user.nameinstead of@user.name.To avoid an N+1 query situation on your users, you could update your controller to use this: