Rails Image Not Showing Up With Paperclip

2019-08-27 02:27发布

问题:

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

回答1:

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.



回答2:

This is tagged as Rails 4, so I think the strong parameter whitelisting in your controller should do fine. Take out the attr_accessor :avatar_file_nameand it should work.