1
votes

Pretty new to ruby on rails and experimenting with some new gems.

So I have an issue with paperclip only loading the default image. No matter if I choose one or not.

Here is my code. Schema

create_table "users", force: true do |t|
t.string   "name"
t.string   "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "password_digest"
t.string   "remember_token"
t.boolean  "admin",              default: false
t.string   "image_file_name"
t.string   "image_content_type"
t.integer  "image_file_size"
t.datetime "image_updated_at"
end

user model:

has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" },      :default_url => "/images/emptyuser.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

call in the show.html.erb

      <p>
        <%= image_tag @user.image.url(:medium) %>
      </p>

user controller

  def create
@user = User.new(user_params)
if @user.save
  sign_in @user
 flash[:success] = "Welcome to My App?!"
  redirect_to @user
else
  render 'new'
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
  flash[:success] = "Profile updated"
  redirect_to @user
else
  render 'edit'
end
    private

def user_params
  params.require(:user).permit(:name, :email, :password,
                               :password_confirmation, :image)
end

I'm pretty sure I had it working, and then when I added the default image I broke it, and now when I go back I can't get it to work..

Anyone know why my images aren't uploading and only the default url is being loaded?

I think I need some sleep. Sorry if this is a dumb fix, but I appreciate your help.

EDIT: Thanks for the link @hunteros I somehow missed that in the doc. It is being saved now to the default location. I will check to see if explicitly changing them does the trick.

EDIT#2: Would this be the proper syntax and pathing? Added to model user.rb

   :path  => ":rails_root/public/system/:class/:image/:id_partition/:style/:filename"
    :url => ":rails_root/public/system/:class/:image/:id_partition/:style/:filename"

The actual file structure is as follows: system/:class/images(is this :image?)/000(not sure here)/001/:style/actual file finally

Seems a little intense. Couldn't they just all go in one damn directory..?

:path  => ":rails_root/public/system/:class/:attachment/:style/:filename",
:url => "/system/:class/:attachment/:style/:filename"

when I call user.image I get the path, and it seems to match up but I still see default i mage. Image URL: /system/users/images/medium/sarahanddee.JPG?1394522170

are the numbers at the end of the string a problem?

2
can you try to add a bit more information about where it might be going wrong? for example, do you see the files actually saved in the /public/system/users/images within your Rails app directory? And what is happening in your controller when you try to handle the user creation? are the users saved properly just without images? - hunteros
also try explicitly specifying the :path and/or :url as explained here to make sure you know where it is trying to save the uploads - hunteros
I notice it now. I'm not sure how to access them via the url => option. I'm not too familiar with rails. See edit #2 in comment - Peege151
In the past, I've used :path => ":rails_root/public/system/:class/:attachment/:style/:filename" and :url => "/system/:class/:attachment/:style/:filename". Changing :image to :attachment and getting rid of :id_partition will clean up the path a little bit. For :url, start your path with the /system rather than :rails_root - hunteros
sorry @Peege151 I meant what values are being saved in the database for those image fields (image_file_name, etc). I believe Paperclip only shows the default image if those are blank; otherwise it will use the specified url, even if it doesn't exist at the specified path (it will just show a broken image in that case, not the default) - hunteros

2 Answers

1
votes

Firstly, never code when you're tired. It's my opinion that you must be well-rested in order to create truly efficient & compelling software

Secondly, you need to test your upload process to see if your image is being uploaded. If it's present in your db, the issue will be with how you're calling the image, else it will be a problem with the upload process

Your default_url looks fine to me - perhaps you could upload some logs of the requests your app is making each time it shows the "default" images?

0
votes

I just had to remove the @ from @user.image.url