Reputation: 693
Rails 3.2.3 with Devise 2.1
I'm just trying to create what I thought would be a simple page that shows all my users. Users are being controlled by Devise. Users have profile images and email addresses. All Devise views are working fine.
I've tried some of the suggestions seen elsewhere, such as defining the user in a new user_controller or in the application_controller but my definitions don't get through to the other pages. I keep getting the same set of error messages.
My routes are working fine. I've got all the carrierwave stuff working properly in the uploader and in the model. I just can't seem to find the "user" outside of any of the Devise stuff. I know I'm being stupid hoping for something obvious to simply be obvious, but . . .
My current error is:
undefined method `image_url' for nil:NilClass
My page for showing Users:
<% provide(:title, 'Our Users') %>
<div class="center hero-unit">
<div>
<h2>Our Users</h2>
<%= render 'shared/user' %>
</div>
</div>
The helper:
<div>
<%= image_tag user.image_url(:thumb).to_s %>
<h5><%= user.email %></h5>
</div>
I've tried a variety of things in a users_controller but nothing seems to work. I've tried adding resources :users to my routes. No help.
Any tips, pointers, etc. would be much appreciated. I've gone 4 hours at this so as not to bug the stackoverflow tribe unnecessarily but I'm stumped.
Upvotes: 1
Views: 6345
Reputation: 767
As an update to this question. The answer provided by @Dougui is currently working for Devise as of today's date. Something may have been updated in the Devise gem since 2012, but now you can define @users = User.all
in the controller to have it accessed in the view. Just make sure that it is placed in a def
for that page. For instance if you have a Pages controller for various pages and you want a rolodex of the users on your site, make sure that the variable is defined under for the rolodex page, like this: (Notice in this example that the variable is only available to the rolodex page.)
app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
end
def contact
end
def rolodex
@users = User.all
end
end
app/views/pages/rolodex.html.erb
<h1>Current User Rolodex</h1>
<% @users.each do |user| %>
<%= image_tag user.image_url(:thumb).to_s %>
<h5><%= user.email %></h5>
<br>
<% end %>
And if you want the @users
variable available to multiple pages, then you can require it at the top of the controller, define it privately and then restrict which pages have access to the variable. Here is an example:
app/controllers/pages_controller.rb
class PagesController < ApplicationController
before_action :get_users, only: [:rolodex, :friends]
def home
end
def contact
end
def rolodex
end
def friends
end
private
def get_users
@users = User.all
end
end
Upvotes: 0
Reputation: 693
Solved it! I was trying to define my User in a non-Devise controller. Then using this in my view:
<% @users.each do |user| %>
<%= image_tag user.image_url(:thumb).to_s %>
<%= user.email %><br/>
<% end %>
I ditched the new user_controller and simply changed my view to this:
<% @users = User.all %>
<% @users.each do |user| %>
<%= image_tag user.image_url(:thumb).to_s %>
<%= user.email %><br/>
<% end %>
Bam! Showed right up.
Upvotes: 6
Reputation: 5933
To print all users set rails partial collection rendering with User.all
provided:
<% provide(:title, 'Our Users') %>
<div class="center hero-unit">
<div>
<h2>Our Users</h2>
<%= render 'shared/user', :collection=>User.all %>
</div>
</div>
Upvotes: 0
Reputation: 7230
Ỳou should find you user just like a normal model, like this :
user = User.find(1)
user.email
For the current user you can do current_user.email
In a contoller you should do this :
@users = User.all
And in your view
<% @users.each do |user| %>
<%= user.email %>
<% end %>
Upvotes: 0