没有出现与回形针的路轨图象
我想在我的主页上显示一个头像。 我正在使用Paperclip来做到这一点。 但是,每当我尝试显示图像时,我都会得到:
这就是我的代码:
视图:
Users
@ user.username确实正确显示。 和我试图展示的任何其他财产一样。 debug语句返回:“— /images/original/missing.png?1400548644”
但是,我可以在这里找到图像:
./public/system/users/avatars/000/000/001/original/myImage.png
但是在这里找不到:
./app/assets/images
是否可能在错误的地方得救? 以下是我的其余代码供参考:
模型:
class User { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 } validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX validates :password, :confirmation => true #password_confirmation attr validates_length_of :password, :in => 6..20, :on => :create before_save :encrypt_password after_save :clear_password def encrypt_password if password.present? self.salt = BCrypt::Engine.generate_salt self.encrypted_password= BCrypt::Engine.hash_secret(password, salt) end end def clear_password self.password = nil end def self.authenticate(username_or_email="", login_password="") if EMAIL_REGEX.match(username_or_email) user = User.find_by_email(username_or_email) else user = User.find_by_username(username_or_email) end if user && user.match_password(login_password) return user else return false end end def match_password(login_password="") encrypted_password == BCrypt::Engine.hash_secret(login_password, salt) end end
控制器:
class UsersController [:new, :create] def index @users = User.all @user = User.find(params[:id]) end def new @user = User.new end def create @user = User.new(user_params) if @user.save flash[:notice] = "You signed up successfully" flash[:color]= "valid" else flash[:notice] = "Form is invalid" flash[:color]= "invalid" end render "new" end private def user_params params.require(:user).permit(:username, :email, :password, :avatar) end end
用户数据库:
class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :username t.string :email t.string :encrypted_password t.string :salt t.timestamps end end end
用户数据库的头像附件:
class AddAttachmentAvatarToUsers < ActiveRecord::Migration def self.up change_table :users do |t| t.attachment :avatar end end def self.down drop_attached_file :users, :avatar end end
我一直坚持这个问题。 任何想法或建议将受到高度赞赏。 谢谢!
编辑:
代码添加头像
意见/用户/新
Sign Up
{:controller => 'users', :action => 'create'}, :html => { :multipart => true }) do |f| %> Username:
Email:
Password:
Password Confirmation:
- *
控制器/ users_controller
def create @user = User.new(user_params) if @user.save flash[:notice] = "You signed up successfully" flash[:color]= "valid" else flash[:notice] = "Form is invalid" flash[:color]= "invalid" end render "new" end
我认为可能与User
模型中的attr_accessor :avatar_file_name
存在冲突。 您正在创建自己的avatar_file_name
属性,并且Paperclip在尝试使用它时可能会遇到冲突。
我建议删除attr_accessor :avatar_file_name
行。
这被标记为Rails 4,所以我认为控制器中的强参数白名单应该可以正常运行。 取出attr_accessor :avatar_file_name
,它应该可以工作。