Rails 图像未显示回形针 [英] Rails Image Not Showing Up With Paperclip
问题描述
我正在尝试让头像显示在我的主页上.我正在使用回形针来做到这一点.但是,每当我尝试显示图像时,我都会得到这个:
这是我的代码的样子:
查看:
头部><身体><h1>用户</h1><表格><tr><th colspan="1"></th></tr><%= image_tag @user.avatar.url %><%=调试@user.avatar.url %><tr><%=@user.username %></tr></html>
@user.username 显示正确.与我尝试显示的任何其他属性一样.调试语句返回:---/images/original/missing.png?1400548644"
但是,我可以在这里找到图片:
./public/system/users/avatars/000/000/001/original/myImage.png
虽然在这里找不到:
./app/assets/images
它是否被保存在错误的地方?这是我的其余代码供参考:
型号:
class User {:中 =>"300x300>", :thumb =>100x100"}, :default_url =>"/images/:style/missing.png"validates_attachment_content_type :avatar, :content_type =>/\Aimage\/.*\Z/验证 :username, :presence =>真,:唯一性 =>真,:长度 =>{ :in =>3..20 }验证 :email, :presence =>真,:唯一性 =>真, :format =>EMAIL_REGEX验证:密码,:确认=>真#password_confirmation attrvalidates_length_of :password, :in =>6..20, :on =>:创造before_save :encrypt_passwordafter_save :clear_passworddef encrypt_password如果密码存在?self.salt = BCrypt::Engine.generate_saltself.encrypted_password= BCrypt::Engine.hash_secret(密码,盐)结尾结尾def clear_passwordself.password = nil结尾def self.authenticate(username_or_email="", login_password="")如果 EMAIL_REGEX.match(username_or_email)用户 = User.find_by_email(username_or_email)别的用户 = User.find_by_username(username_or_email)结尾如果用户 &&user.match_password(登录密码)返回用户别的返回假结尾结尾def match_password(login_password="")encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)结尾结尾
控制器:
class UsersController <应用控制器before_filter :save_login_state, :only =>[:新建,:创建]定义索引@users = User.all@user = User.find(params[:id])结尾定义新@user = User.new结尾定义创建@user = User.new(user_params)如果@user.saveflash[:notice] = "您注册成功"flash[:color]="有效"别的flash[:notice] = "表单无效"flash[:color]="无效"结尾呈现新"结尾私人的定义用户参数params.require(:user).permit(:username,:email,:password,:avatar)结尾结尾
用户数据库:
class CreateUsers <ActiveRecord::迁移定义改变create_table :用户做 |t|t.string : 用户名t.string : 电子邮件t.string :encrypted_passwordt.string :saltt.时间戳结尾结尾结尾
用户数据库的头像附件:
class AddAttachmentAvatarToUsers <ActiveRecord::迁移def self.upchange_table : 用户做 |t|t.附件:头像结尾结尾def self.downdrop_attached_file :users, :avatar结尾结尾
我已经被这个问题困住了很长一段时间.任何想法或建议将不胜感激.谢谢!
添加头像的代码
观看次数/用户/新
<% @page_title = "foos-tracker | 注册" %><div class="Sign_Form"><h1>注册</h1><%= form_for(:user, :url => {:controller => 'users', :action => 'create'}, :html => { :multipart => true }) do |f|%><p>用户名:</br><%= f.text_field :username%></p><p>电子邮件:</br><%= f.text_field :email%></p><p>密码:</br><%= f.password_field :password%></p><p>密码确认:</br><%= f.password_field :password_confirmation%></p><%= f.file_field :avatar %><%= f.submit :Signup %><%结束%><% if @user.errors.any?%><ul class="Signup_Errors"><% for message_error in @user.errors.full_messages %><li>* <%= message_error %></li><%结束%><%结束%>
controllers/users_controller
def 创建@user = User.new(user_params)如果@user.saveflash[:notice] = "您注册成功"flash[:color]="有效"别的flash[:notice] = "表单无效"flash[:color]="无效"结尾呈现新"结尾
我认为可能与 User
模型中的 attr_accessor :avatar_file_name
存在冲突.您正在创建自己的 avatar_file_name
属性,也许 Paperclip 在尝试使用该属性时遇到了冲突.
我建议删除 attr_accessor :avatar_file_name
行.
I am trying to get an avatar to show up on my main page. I am using Paperclip to do this. However, whenever I try to show the image I get this:
This is what my code looks like:
View:
<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>
The @user.username does display correctly. As does any other property I try to display. The debug statement returns with: "--- /images/original/missing.png?1400548644"
However, I can find the images here:
./public/system/users/avatars/000/000/001/original/myImage.png
It cannot be found here, though:
./app/assets/images
Is it getting saved in the wrong place perhaps? Here is the rest of my code for reference:
Model:
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
Controller:
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
Users Database:
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
Avatar Attachment to users database:
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
I have been stuck on this problem for quite a while. Any ideas or suggestions would be highly appreciated. Thanks!
EDIT:
Code to add avatar
views/user/new
<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
I think there may be a conflict with your attr_accessor :avatar_file_name
in the User
model. You're creating your own avatar_file_name
attribute and perhaps Paperclip is hitting a conflict when trying to use that itself.
I suggest removing the attr_accessor :avatar_file_name
line.
这篇关于Rails 图像未显示回形针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!