【发布时间】:2014-05-31 14:32:58
【问题描述】:
我正在尝试让头像显示在我的主页上。我正在使用回形针来执行此操作。但是,每当我尝试显示图像时,我都会得到以下信息:
这是我的代码的样子:
查看:
<head>
</head>
<body>
<h1>Users</h1>
<table>
<tr>
<th colspan="1"></th>
</tr>
<%= image_tag @user.avatar.url %>
<%= debug @user.avatar.url %>
<tr>
<%= @user.username %>
</tr>
</table>
</body>
</html>
@user.username 确实显示正确。与我尝试显示的任何其他属性一样。 调试语句返回:“--- /images/original/missing.png?1400548644”
但是,我可以在这里找到图片:
./public/system/users/avatars/000/000/001/original/myImage.png
但是在这里找不到:
./app/assets/images
它可能保存在错误的地方吗? 这是我的其余代码供参考:
型号:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessor :avatar_file_name
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
has_attached_file :avatar, :styles => { :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 < ApplicationController
before_filter :save_login_state, :only => [: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
我已经被这个问题困扰了很长一段时间。任何想法或建议将不胜感激。谢谢!
编辑:
添加头像的代码
观看次数/用户/新用户
<body>
<% @page_title = "foos-tracker | Signup" %>
<div class="Sign_Form">
<h1>Sign Up</h1>
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}, :html => { :multipart => true }) do |f| %>
<p> Username:</br> <%= f.text_field :username%> </p>
<p> Email:</br> <%= f.text_field :email%> </p>
<p> Password:</br> <%= f.password_field :password%></p>
<p> Password Confirmation:</br> <%= f.password_field :password_confirmation%> </p>
<%= f.file_field :avatar %>
<%= f.submit :Signup %>
<% end %>
<% if @user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in @user.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
</ul>
<% end %>
</div>
</body>
controllers/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
【问题讨论】:
-
根据 Paperclip docs,图像的文件夹路径看起来正确。
medium和thumb版本是否还有文件夹? -
是的。以及两者的图片。
-
如果你访问
http://<your-domain>/system/users/avatars/000/000/001/original/myImage.png会发生什么?你得到图像了吗? -
如果我尝试导航到该页面,我会收到路由错误。没有路线匹配 [GET] "/system/users/avatars/000/000/001/original/myImage.png"
-
您应用根目录中
ls -la public/system/users/avatars/000/000/001/original/myImage.png的响应是什么?
标签: ruby-on-rails ruby ruby-on-rails-4 paperclip