没有出现与回形针的路轨图象

时间:2014-05-31 14:32:59

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip

我想在我的主页上显示一个头像。我正在使用Paperclip来做到这一点。但是,每当我尝试显示图像时,我都会得到这个:

enter image description here

这就是我的代码:

查看:     

  <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确实正确显示。和我试图展示的任何其他财产一样。 debug语句返回:“--- /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>

控制器/ 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

2 个答案:

答案 0 :(得分:2)

我认为attr_accessor :avatar_file_name模型中的User可能存在冲突。您正在创建自己的avatar_file_name属性,也许Paperclip在尝试使用它时可能会发生冲突。

我建议删除attr_accessor :avatar_file_name行。

答案 1 :(得分:-1)

这被标记为Rails 4,所以我认为控制器中的强参数白名单应该可以正常运行。取出attr_accessor :avatar_file_name它应该可以。