How to set up a paper clip to save to another directory depending on the environment

I collaborate with the application, and sometimes I encounter problems related to the fact that the original developer worked in Linux, and I in OSX. My current problem is loading images using ImageMagik and paperclip. When I try to load pic in the application, I get the following message.

Permission denied - /assets 

I am sure that this means that the application wanted to save the image somewhere in the resource directory, but could not, because it does not exist on my machine.

In a model, this is code that refers to photographs.

 has_attached_file :avatar, :url => "/avatars/:id?style=:style", :styles => { :large => "190x190#", :medium => "70x70#", :thumb => "106x106#" }, :path => "/assets/rob/images/Users/:id/:style/:basename.:extension" 

Now I assume that this directory exists on the programmer's source computer and wherever the site is located. But I do not have this directory, so I did this:

  :path => "~/robotimus/dev_images/:id/:style/:basename.:extension" 

But now I am in pickling, as in the end I will have to return this line before I turn around. As a solution, I could write such a method.

 def images_path Rails.env.production? ? "/assets/rob/images/Users/" : "~/robotimus/dev_images" end 

And then the path line will look like this:

  :path => images_path + "/:id/:style/:basename.:extension" 

Does that sound like a good idea? Also, where should this method be stored? I assume it belongs to config / environment.rb, but I would like to get an expert opinion.

+4
source share
1 answer

I did a similar thing for the project some time ago, where during the development process I saved the images locally, but in the production environment they were saved on Amazon S3.

I did what config/paperclip.yml .

 # config/paperclip.yml common: &common :styles: :thumb: "50x50#" :small: "80x80#" :medium: "200x150#" :normal: "320x240#" :large: "800x600#" :default_url: "/images/default_image.png" development: <<: *common production: <<: *common :storage: :s3 :bucket: "your-bucket-name" :path: "/:some/:path/:id" :url: "s3_domain_url test: <<: *common 

Then I loaded this file into my application configuration:

 # config/initializers/config.rb require 'ostruct' def load_config_yaml(config_file) YAML.load(File.read(Rails.root.join('config', config_file)))[Rails.env] end AppConfig = OpenStruct.new(load_config_yaml('application.yml')) AppConfig.paperclip = load_config_yaml('paperclip.yml') 

Then I just provided a has_attached_file call to AppConfig.paperclip .

 # app/models/image.rb class Image < ActiveRecord::Base has_attached_file :photo, AppConfig.paperclip end 

So, if you want to store files in a different way (but still locally), just do not use s3-stuff.

Of course, you can skip most of the configuration contents and just set the image path in the config somewhere if you don't want to go all-in for this idea, but I like to have separate settings.

+10
source

Source: https://habr.com/ru/post/1433441/


All Articles