Ruby on Rails - undefined method - Model can't be accessed by Controller -


solved - i've added if statement in html.erb before displaying profile avatar , it's appearing. thank mark , holger help.

i'm trying make users appear in league table i'm getting error message saying there undefined method in leaguescontroller file. i've tried following class names controller file - leaguescontroller < application controller , leaguescontroller < userscontroller (i know method has been defined in class, post both below). 'index' matches html.erb file name.

leaguescontroller (i've tried user.includes(:profile).order(etc) still doesn't pick 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 , :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 find url path should used ,      # dynamic ids need included. use params in relevant controller file     # include paramater.    # /users/:id   def show      # "user" refering model same name -> user.rb     @user = user.find( params[:id] )   end  end 

what's strange code used work fine. i've compared previous commits before stopped working , seem match perfectly. when cloned commit, worked fine don't know start looking error. don't have model file leagues never have done , used work fine. i've never included leagues in belongs_to/has_many associations again, used work fine...

here routes file too.

routes.rb

rails.application.routes.draw     root to: 'pages#home'     devise_for :users, controllers: { registrations: 'users/registrations' }     resources :users         # singular resource , profile because requiring 1 resource , profile per user        resource :profile     end     resources :leagues     'about', to: 'pages#about'     resources :contacts, only: :create     '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 compare top 5 in 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 |user, index| %>             <% if index < 5 %>               <tr class="tr">                 <td style="padding-top: 20px;"><%= index+1 %></td>                 <td class="left-column"><%= link_to user %><%= image_tag user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user %><%= 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) %><%= image_tag current_user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user_path(current_user.id) %><%= 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 |user, index| %>             <% if index < 5 %>               <tr class="tr">                 <td style="padding-top: 20px;"><%= index + 1 %></td>                 <td class="left-column"><%= link_to user %><%= image_tag user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user %><%= 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) %><%= image_tag current_user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user_path(current_user.id) %><%= 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 "/leagues" xx.xxx.xx.xxx @ 2017-03-28 14:23:54 +0000 cannot render console xx.xxx.xx.xxx! allowed networks: xxx.x.x.x, ::1, xxx.x.x.x/xxx.xxx.xxx.xxx processing leaguescontroller#index html   user load (0.3ms)  select  "users".* "users" "users"."id" = ? order "users"."id" asc limit ?  [["id", 3], ["limit", 1]]   user load (0.3ms)  select "users".* "users" order last_sign_in_at desc   profile load (0.2ms)  select "profiles".* "profiles" "profiles"."user_id" in (9, 3, 8, 7, 6, 5, 2, 4, 1)   user load (0.3ms)  select "users".* "users" order sign_in_count desc   profile load (0.2ms)  select "profiles".* "profiles" "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".* "users" order last_sign_in_at desc   cache (0.0ms)  select "profiles".* "profiles" "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' 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 %><%= image_tag user.profile.avatar.url, class: 'sml-avatar-show-img' %><% end %> <%= link_to user %><%= 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 see other files, let me know in comments. appreciate help/advice this. time.

i can't see view file, i'm assuming you're calling .avatar on each user or on profile?

if change user.avatar or user.profile.avatar to:

user.avatar if user.avatar 

or

user.profile.avatar if user.profile && user.profile.avatar 

then should clear error , receive blank area avatar should be.

edit: after looking @ view file, 1 of these 2 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 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 %> 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -