Try this:
<%= image_tag avatar_url(current_user), :class => 'gravatar' %>
题
I've set up gravatar and have got it working for my 'users/*user id goes here*'.
But whenever I try to use it in dashboard/index
that's whenever it gives me the error
Undefined method 'email' for nil:NilClass
My dashboard controller is :
class DashboardController < ApplicationController
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
end
Dashboard View :
<div class="dash-well">
<div class="gravatar-dashboard">
<%= image_tag avatar_url(@user), :class => 'gravatar' %>
<h1 class="nuvo wtxt"><%= current_user.username.capitalize %></h1>
</div>
</div>
My Application Helper :
module ApplicationHelper
def avatar_url(user)
default_url = "#{root_url}images/guest.png"
gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
"http://gravatar.com/avatar/#{gravatar_id}.png?s=200{CGI.escape(default_url)}"
end
def avatar_url_small(user)
default_url = "#{root_url}images/guest.png"
gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
"http://gravatar.com/avatar/#{gravatar_id}.png?s=40{CGI.escape(default_url)}"
end
end
My user model :
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :user_id, :id, :website, :bio, :skype, :dob, :age
has_many :posts
# attr_accessible :title, :body
end
My Dashboard Model :
class Dashboard < ActiveRecord::Base
attr_accessible :status, :author, :email, :username, :id, :user_id, :user, :website, :bio, :skype, :dob, :age
belongs_to :user
end
Sorry, i'm pretty new to Ruby-On-Rails!
解决方案
Try this:
<%= image_tag avatar_url(current_user), :class => 'gravatar' %>
其他提示
You really want this in your controller:
def index
@user = current_user
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
Note the addition of the second line, which assigns the @user variable to the current_user.
Then, the @user you are calling in your view will work. A typical rails pattern that you'll see emerge as you continue to use it is that most variable that begin with the @ symbol will be defined in the corresponding controller method for that view. So, if you are using a variable with @, and it's not available, check the controller to ensure it's defined first. (FYI these are called instance variables if you want to learn more).
To address a second concern, if you are current_user and you wanted to visit another user's page:
def show
@user = User.find params[:id]
respond_to do |format|
format.html # index.html.erb
format.json { render json: @user }
end
end
This would work with a URL like /users/1, you can use that very same call to avatar_url, pass the @user, and it will get that user's avatar, where the user is the one that matches the given user ID. You probably already have this exact code in your controller already, but hopefully now you see why it works.
Good luck!