SOLVED - I've added an if statement in the HTML.erb before displaying the profile avatar and it's now appearing. Thank you Mark and Holger for your help.
I'm trying to make my users appear in a league table but I'm getting an error message saying there is an undefined method in my LeaguesController file. I've tried the following class names for my Controller file - LeaguesController < Application Controller and LeaguesController < UsersController (I know the method has been defined in this class, will post both below). 'index' matches the html.erb file name.
LeaguesController (I've tried User.includes(:profile).order(etc) but still doesn't pick up the user fields)
class LeaguesController < ApplicationController
def index
@LMSusers = User.order('last_sign_in_at DESC')
@MVPusers = User.order('sign_in_count DESC')
@LMSuser_position = User.order('last_sign_in_at DESC').find_index(current_user)
@MVPuser_position = User.order('sign_in_count DESC').find_index(current_user)
end
end
User Model file
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Association - Ruby Documentation
belongs_to :plan
has_one :profile
end
UsersController
class UsersController < ApplicationController
before_action :authenticate_user!
def index
@users = User.includes(:profile)
end
# Use rails routes in console to find what URL path should be used and
# what dynamic ids need to be included. Use params in relevant controller file
# to include the paramater.
# GET to /users/:id
def show
# "User" is refering to model with same name -> user.rb
@user = User.find( params[:id] )
end
end
What's really strange is this code used to work fine. I've compared it to the previous commits before it stopped working and they seem to match up perfectly. When I cloned the commit, that also worked fine so I really don't know where to start looking for this error. I don't have a model file for Leagues but I never have done and it used to work fine. I've never included Leagues in any belongs_to/has_many associations but again, it used to work fine...
Here is the routes file too.
Routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
devise_for :users, controllers: { registrations: 'users/registrations' }
resources :users do
# Singular resource and profile because requiring only one resource and profile per user
resource :profile
end
resources :leagues
get 'about', to: 'pages#about'
resources :contacts, only: :create
get 'contact-us', to: 'contacts#new', as: 'new_contact'
# Nested Resources Documentation
end
HTML.erb file
<div>
<h1 class="text-center">The Leaderboard</h1></br>
<p class="text-center">Check how you compare to the top 5 in the community.</p></br>
</div>
<div class="container">
<div class="row">
<div class="col-md-6">
<div>
<table class="table">
<tr>
<th class="league-header" colspan="6">Last Man Standing</th>
</tr>
<tr class="row-header">
<th>Position</th>
<th>Name</th>
<th class="left-column">Job Title</th>
<th class="left-column">Last Logged In</th>
</tr>
<% @LMSusers.each_with_index do |user, index| %>
<% if index < 5 %>
<tr class="tr">
<td style="padding-top: 20px;"><%= index+1 %></td>
<td class="left-column"><%= link_to user do %><%= image_tag user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user do %><%= user.profile.first_name %> <%= user.profile.last_name %><% end %></td>
<td style="padding-top: 20px;" class="left-column"><%= user.profile.job_title %></td>
<td style="padding-top: 20px;" class="left-column"><%= user.last_sign_in_at.strftime("%d/%m/%Y") %></td>
</tr>
<% end %>
<% end %>
<% if user_signed_in? %>
<% if current_user.profile %>
<tr class="tr user-tr">
<td style="padding-top: 20px;" ><%= @LMSuser_position + 1 %></td>
<td class="left-column"><%= link_to user_path(current_user.id) do %><%= image_tag current_user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user_path(current_user.id) do %><%= current_user.profile.first_name %> <%= current_user.profile.last_name %><% end %></td>
<td style="padding-top: 20px;" class="left-column"><%= current_user.profile.job_title %></td>
<td style="padding-top: 20px;" class="left-column"><%= current_user.last_sign_in_at.strftime("%d/%m/%Y") %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
</div>
<div class="col-md-6">
<div>
<table class="table">
<tr>
<th class="league-header" colspan="6">MVP</th>
</tr>
<tr class="row-header">
<th>Position</th>
<th>Name</th>
<th class="left-column">Job Title</th>
<th class="left-column">Number of Logins</th>
</tr>
<% @MVPusers.each_with_index do |user, index| %>
<% if index < 5 %>
<tr class="tr">
<td style="padding-top: 20px;"><%= index + 1 %></td>
<td class="left-column"><%= link_to user do %><%= image_tag user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user do %><%= user.profile.first_name %> <%= user.profile.last_name %><% end %></td>
<td style="padding-top: 20px;" class="left-column"><%= user.profile.job_title %></td>
<td style="padding-top: 20px;" class="left-column"><%= user.sign_in_count %></td>
</tr>
<% end %>
<% end %>
<% if user_signed_in? %>
<% if current_user.profile %>
<tr class="tr user-tr">
<td style="padding-top: 20px;" ><%= @MVPuser_position + 1 %></td>
<td class="left-column"><%= link_to user_path(current_user.id) do %><%= image_tag current_user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user_path(current_user.id) do %><%= current_user.profile.first_name %> <%= current_user.profile.last_name %><% end %></td>
<td style="padding-top: 20px;" class="left-column"><%= current_user.profile.job_title %></td>
<td style="padding-top: 20px;" class="left-column"><%= current_user.sign_in_count %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
</div>
</div>
</div>
Error Log
Started GET "/leagues" for xx.xxx.xx.xxx at 2017-03-28 14:23:54 +0000
Cannot render console from xx.xxx.xx.xxx! Allowed networks: xxx.x.x.x, ::1, xxx.x.x.x/xxx.xxx.xxx.xxx
Processing by LeaguesController#index as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY last_sign_in_at DESC
Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" IN (9, 3, 8, 7, 6, 5, 2, 4, 1)
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY sign_in_count DESC
Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" IN (3, 1, 2, 5, 7, 4, 6, 8, 9)
Rendering leagues/index.html.erb within layouts/application
CACHE (0.0ms) SELECT "users".* FROM "users" ORDER BY last_sign_in_at DESC
CACHE (0.0ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" IN (9, 3, 8, 7, 6, 5, 2, 4, 1)
Rendered leagues/index.html.erb within layouts/application (76.6ms)
Completed 500 Internal Server Error in 88ms (ActiveRecord: 1.5ms)
ActionView::Template::Error (undefined method `avatar' for nil:NilClass):
21: <% if index < 5 %>
22: <tr class="tr">
23: <td style="padding-top: 20px;"><%= index+1 %></td>
24: <td class="left-column"><%= link_to user do %><%= image_tag user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user do %><%= user.profile.first_name %> <%= user.profile.last_name %><% end %></td>
25: <td style="padding-top: 20px;" class="left-column"><%= user.profile.job_title %></td>
26: <td style="padding-top: 20px;" class="left-column"><%= user.last_sign_in_at.strftime("%d/%m/%Y") %></td>
27: </tr>
app/views/leagues/index.html.erb:24:in `block (2 levels) in _app_views_leagues_index_html_erb___1987220472087352031_70206048257300'
app/views/leagues/index.html.erb:24:in `block in _app_views_leagues_index_html_erb___1987220472087352031_70206048257300'
app/views/leagues/index.html.erb:20:in `each_with_index'
app/views/leagues/index.html.erb:20:in `_app_views_leagues_index_html_erb___1987220472087352031_70206048257300'
Rendering /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
Rendering /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (7.2ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.7ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (38.7ms)
If you'd like to see any other files, let me know in the comments. Would appreciate any help/advice with this. Thanks for your time.
I can't see your view file, but I'm assuming you're calling .avatar on each user or on their profile?
If you change user.avatar or user.profile.avatar to:
user.avatar if user.avatar
or
user.profile.avatar if user.profile && user.profile.avatar
then you should clear your error and just receive a blank area where their avatar should be.
EDIT: After looking at the view file, one of these two lines:
<%= image_tag user.profile.avatar.url, class: 'sml-avatar-show-img' %>
<%= image_tag current_user.profile.avatar.url, class: 'sml-avatar-show-img' %>
Is apparently throwing the error. Change them to:
<%= (image_tag user.profile.avatar.url, class: 'sml-avatar-show-img') if user.profile && user.profile.avatar %>
<%= (image_tag current_user.profile.avatar.url, class: 'sml-avatar-show-img') if current_user && current_user.profile && current_user.profile.avatar %>