我最近開始在rails上學習ruby,並且我能夠成功創建應用程序並使用設計添加用戶,還用回形針給用戶添加了頭像。Ruby on Rails無法在應用程序中顯示回形針頭像
現在我遇到了如何在整個應用程序中顯示頭像的問題。頭像只顯示http:localhost:3000/users/...
用於爲例(在色器件文件夾內),但如果我嘗試創建一個新的頁面,模式,控制器http://localhost:3000/profile/
爲爲例,使用標籤
<%= image_tag @user.avatar.url(:thumb) %>
的頁面將不會加載和意志返回這個錯誤
undefined method 'avatar?' for nil:NilClass
這可能是非常簡單的東西,但我不知道如何解決它。
我的模型user.rb
看起來是這樣的:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_uniqueness_of :username
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
attr_accessible :name, :username, :email, :password, :password_confirmation, :remember_me, :avatar
attr_accessor :current_password
end
我的控制器看起來像這樣:
class UserController < ApplicationController
def profile
end
end
謝謝!
這裏絕對需要更多的上下文。乍一看,它看起來像'@ user'變量沒有在您的配置文件控制器中定義。請發佈你的'Profile'模型和控制器的代碼。 – jordanpg
謝謝。我剛剛用我的模型和控制器更新了這個問題。希望這個幫助。 –
你首先需要找到方法'avatar?'被調用的位置(注意這與你在樣本中顯示的方法'avatar'不同'<%= image_tag @ user.avatar.url(:thumb)% >'。 – cdesrosiers