ruby on rails - Undefined method `avatar' for nil:NilClass -
i'm trying print image using paperclip gem.
when print using views/devise/edit.html.rb looks when want print in views/layout/application.html.rb i'm getting error in console:
actionview::template::error (undefined method `avatar' nil:nilclass): 7: <%= csrf_meta_tags %> 8: </head> 9: <body> 10: <%= image_tag @user.avatar.url(:thumb) %> 11: <%= yield %> 12: 13: </body> app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb___4291648939640547438_70254980514000'
this user.rb:
class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_attached_file :avatar, :styles => { :medium => ["300x300>"], :thumb => ["100x100>"], :page => ["800"] } end
this application_controller.rb:
class applicationcontroller < actioncontroller::base # prevent csrf attacks raising exception. # apis, may want use :null_session instead. before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation) } devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :avatar) } end end
@user
here undefined. can define in controller or use devise's current_user
helper.
replacing:
@user.avatar.url(:thumb)
with:
current_user.avatar.url(:thumb)
should past error long user signed in when seeing view. otherwise you'll need check nil before calling #avatar.
Comments
Post a Comment