Reputation: 827
The error I am getting is this: Couldn't find User without an ID
I am effectively trying to have a user's courses be listed out on their home page when they sign in. However I can't get the @ user to work properly in the controller, and I believe its a routing error but not sure
Static Pages Controller
class StaticPagesController < ApplicationController
def home
@user = User.find(params[:id]) <<<here is the problem
@courses = @user.courses
end
end
View Partial
<div class="row">
<div class="span8">
<% if @courses.any? %>
<ul class="courses">
<%- @courses.each do |course| %>
<%= link_to course.title, course_path(course) %>
<%- end%>
</ul>
<% end %>
</div>
</div>
Routes.rb
Serenity::Application.routes.draw do
root to: 'static_pages#home'
# match '/signup', to: 'users#new'
# match '/signin', to: 'sessions#new'
# match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users
ActiveAdmin.routes(self)
resources :users do
member do
get :courses
end
end
resources :courses
resources :assignments, only: [:create, :destroy]
Upvotes: 0
Views: 687
Reputation: 4375
You do not have any params
on your home page. Use the built-in current_user
method from Devise.
Upvotes: 1
Reputation: 1885
Looking at your routes, I can see you're using Devise
for authentication. This means you most likely have already a helper called current_user
giving you the User
currently signed in.
I'm guessing you're trying to display on the home page all the courses of the current user. So change your controller action like this:
class StaticPagesController < ApplicationController
def home
@courses = current_user.courses
end
end
Upvotes: 0
Reputation: 29399
You've got your root page going to static_pages#home
, but there is no id
parameter in that case, so params[:id]
is returning nil
, so that's what's being passed to find
. Assuming you want to access the id
of the currently logged in user, you need to get at it some other way, as discussed in http://ruby.railstutorial.org/book/ruby-on-rails-tutorial. Search for current_user
in that tutorial.
Upvotes: 1