show image using absolute path rails - ruby-on-rails

For Ruby on Rails I want to use
<img src =../../../photo/1.PNG >
to show some images stored outside my app folder. I use this and use .. to point out where image it it , But it does not work。 I use Win7, Is there anyone could help me ?
I have a MVC named "showcase" generated by scaffold, and I wanna to have the same showcase in two apps.
In app1: showcase (where I wanna to share the images)model: house is the image name.
class Showcase < ActiveRecord::Base
attr_accessible :house,:sequence
has_attached_file :house, :whiny => false,
:styles => { :large => "350x300>",
:medium => "120x100>",
:thumb => "50x50>"}
end
then the images will be uploaded in the folder: public/systems/showcases/houses/000/000/0001/1.PNG
in app2. I create a showcase model with migration. and use ActiveResources share the database between app1 and app2. but app2 can not configure paperclip type.
class Showcase < ActiveResource::Base
self.site = "http://localhost:3000"
end
how can I in app2, call images uploaded from app1.

If your trying to really share images between applications, and they are uploads and not static assets then create a systems folder somewhere else, then make the system folders in each app a symlink to the share folder. That way your not having crazy url paths, and they both can asses it, and you wont end up with dupes or mismatches.

You should really use the rails assets pipeline if you are using rails 3 or above. Put your images in app/assets/images and call the rails functions that gives you url to the assets e.g. <%= image_tag "rails.png" %>.
For more detail read the following guideline:
http://guides.rubyonrails.org/asset_pipeline.html

Related

Rails: ckeditor gem not working in production mode with nginx and passenger

I am using the ckeditor WYSIWYG text editor in my rails project. In particular: I am having trouble configuring for image uploads in production mode.
It works fantastic in development mode, and even locally in production mode while using Puma. When I click to Upload a photo and click the Browse Server button. It immediately looks for photos where I expect them to: within /assets/ckeditor_assets/pictures.
The issue is getting it to work in production mode with nginx. When I use nginx in production mode: it returns a 404 Not Found error message. I looked at my server logs and here is what it says:
"/var/www/MYAPP/ckeditor/pictures" failed (2: No such file or directory)
So for some reason it is attempting to find a ckeditor directory within my public directory (because that is a symlink to my public directory). I am not even sure why ckeditor is looking for a ckeditor directory when it should instead be looking within my apps public/assets/ckeditor_assets directory.
I attempted to fix this by creating a ckeditor directory within my public directory, and then putting a pictures directory inside of there. However, when I did that I got a 403 Forbidden error.
I did notice that within the config/initializers/ckeditor.rb file there is this line:
# Customize ckeditor assets path
# By default: nil
#config.asset_path = "http://www.example.com/assets/ckeditor/"
So just to give it a shot I hardcoded where I wanted ckeditor to go and fetch the pictures, but unfortunately that didn't work either.
Any suggestions please let me know. Thanks! I will go ahead and show my ckeditor::picture model file in case that provides any clues:
class Ckeditor::Picture < Ckeditor::Asset
has_attached_file :data,
:url => "/assets/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:path => ":rails_root/public/assets/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:styles => { :content => '800>', :thumb => '118x100#' }
validates_attachment_presence :data
validates_attachment_size :data, :less_than => 2.megabytes
validates_attachment_content_type :data, :content_type => /\Aimage/
def url_content
url(:content)
end
end
A buddy of mine figured it out. It turns out it was simply a configuration required in the nginx.conf file. I'm using nginx and passenger.
ck_editor sends a request to ckeditor/pictures as opposed to MYAPP/ckeditor/pictures. Basically: it ignores the relative path (via passenger) of your app. I just updated the location block regex like so:
# nginx.conf
...
location ~ ^/(RELATIVE_PATH_FOR_APP|ckeditor/pictures)(/.*|$) {
...
}
An alternative is that one might be able to forgo the regex for ckeditor/pictures within the location block by overriding some of the configurations found in the ckeditor config.js file. That way you are specifying the ck_editor routing at the application level as opposed to the server level.

Broken images when seeding database on heroku

I have a Rails 4 application that uses paperclip to attach photos. My db/seeds.rb file adds some photos for my Person model with lines like this:
Person.create(:first_name => 'Jon', :last_name => 'Snow',
:photo => File.open("#{Rails.root}/app/assets/images/jon-snow.png))
In my app/models/person.rb file I have the :photo as a paperclip attachment where it is cropped and resized:
class Person < ActiveRecord::Base
has_attached_file :photo,
:styles => { :medium => "256x256#", :small => "64x64#", :tiny => "24x24#" },
:default_url => :set_default_avatar,
:url => "/assets/photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/photos/:id/:style/:basename.:extension"
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
My problem is that when I try to deploy this to heroku (with the Cedar stack) and then seed the database, the images all come up as broken. However, on my local computer everything comes up completely fine.
The broken image tag that is generated on heroku might look something like:
<img alt="Jon Snow" src="/assets/photos/21/small/jon-snow.png?1386825683">
Does anyone know why this link would break on heroku but not on my computer? I know that heroku is generating the resized photos because the heroku console outputs things similar to what is in this post: Seed images in heroku with paperclip.
However, it is putting them into public/assets instead of in public/assets/photos/:id/:style/:basename.:extension as the controller specifies.
I've tried doing what that linked post mentions, as well as running:
heroku run rake assets:precompile
and a number of other things, but nothing works. Any help would be greatly appreciated.
The issue is the same I described in this answer How to use paperclip with rails and how does it work in deployment?
The legacy Bamboo stack had a read-only file system so you were unable to write on the file-system.
In the new Cedar stack, the file system is no longer read-only, so the upload will not fail. However, you should keep using AWS or any other external storage because Heroku distributes your compiled application across several machines, and it's not guaranteed that the image will be visible from another request. They call it ephemeral filesystem.
In other words, you should keep using AWS or any other storage outside Heroku file-system.
From heroku-dev
The filesystem for the slug is read-only, which means you cannot
dynamically write to the filesystem for semi-permanent storage. The
following types of behaviors are not supported: Caching pages in the
public directory Saving uploaded assets to local disk (e.g. with
attachment_fu or paperclip)
Use AWS S3

Rails : file upload using paperclip plugin - changing the default location where files are stored

Hi Id like to change the default location that paperclip saves its files to .
From the documentation I see that the default location is at
rails_root/public/system/users/images/.....
Id like to save the files to
rails_root/assets/images/uploads/.....
So I changed the User model as follows:
class User < ActiveRecord::Base
has_attached_file :image , :url => ":rails_root/assets/images/uploads/:id/:style/:basename.:extension",
:path => ":rails_root/assets/images/uploads/:id/:style/:basename.:extension"
end
However, this did not work. When I upload the file and go to the "show" page I see the following screen.
As you can see the user is created however. The image link is broken and when I click on the link
I get this message on the web page
No route matches [GET] "/Users/AM/Documents/RailsWS/bmc_mam/assets/images/uploads/13/original/bmc_TransparentFinal.png"
Can someone please help me understand this error
The images won't be served directly from assets without you having to do some configuration. They would need to be at Rails.root/public/assets/images/uploads/etc. to be seen via browser. In a browser, the path to the image would be something like http://foo.com/assets/images/uploads/etc.. public will be stripped off by default. Take a look at http://guides.rubyonrails.org/getting_started.html#creating-the-blog-application for some information on the default directory structure of a Rails app.

How to store images in db_files in attachment_fu plugin?

In my rails application I have used attachment_fu plugin for image upload.
I am new on rails, I want to store file in attachment_fu db_files table.
How to store images in db_files and which path i want to specify to display images?
Thanks
Unfortunately attachment_fu does not support the same attachment "interface" for :storage => :db_file as it does for :storage => :file_system, but there's a plugin that fills the void here :
https://github.com/kares/attachment_fx
Extends the file interface for the
:db_file backend (#see attachment_fu
:storage option). The database backend
interface mimics the :file_system
storage, the db data is on-demand
downloaded into the public directory
(the target path prefix is
customizable with the :path_prefix
option).
You basically use the :file_system methods like You're used to with the :db_file backend e.g. photo.public_filename(:small). As a bonus You can enjoy some useful helpers from the owning model :
user.has_photo?
user.photo_path(:small)
user.photo_full_path

Uploading a file to a directory outside of RAILS ROOT with attachment_fu

I'm trying to use attachment_fu to upload files to a directory outside of the RAILS_ROOT. I want the files to be saved to ~/APP_NAME/uploads/ so that they can be approved/rejected before becoming publicly available. I've tried the following configuration for has_attachment:
has_attachment :storage => :file_system,
:path_prefix => "~/APP_NAME/uploads/",
:max_size => 5.megabytes
Unfortunately, this configuration simply creates the ~/APP_NAME/uploads/ directory structure in RAILS_ROOT. Any way to save the file outside of RAILS_ROOT?
This probably isn't an Attachment-Fu issue, but rather how Ruby handles File I/O as well as how files are stored in Unix.
So for instance if your app lives in, say, ~/Users/ron/APP_NAME
If you change the above code:
:path_prefix => "~/APP_NAME/uploads/"
To:
:path_prefix => "../#{RAILS_ROOT}/uploads"
The files would be stored in a folder called "uploads" in ~/Users/ron/uploads. The "../" means one directory above the current Rails root. If you want to go up two directories, it would be "../../" and so on.
But that only addresses hierarchical navigation. If you wanted to tell Attachment-Fu to store files in a hardcoded directory in your filesystem, you could give it a file path such as "~/Users/ron/APP_NAME/uploads", but keep in mind hardcoding in a file path this way is brittle and could be a pain point in the future should your file storage requirements change.
Hope that helps.
I found an alternative method that suits me better than using relative pathnames. I added a method called full_filename to my attachment class:
class attachment < ActiveRecord::Base
def full_filename
return "/Users/ron/attachments/#{id}.#{file_format}"
end
end

Resources