Rails File.exist? yields false when file is present - ruby

I've found a few posts here regarding File.exists? in a Rails app but trying the solutions have not helped, I'm fairly new so I must be doing something dumb.
I'm using:
Rails 3.2.11
Ruby 1.9.3
Paperclip for the file mentioned below
ActiveAdmin to upload the file mentioned below
Working in development environment
Assets not precompiled
I have a model "style" and it has an image attachment, I can render the image with
<%= image_tag(#style.style_image) %>
and it works just fine.
In short, I want to check if the file image is actually there in the folder it should be in - I don't want to use #style.style_image.present? for checking images because that just checks the db record. I want to use File.exist? to see if there's actually a file for #style.style_image.
So in my view file, I have the code
<% if File.exist?(#style.style_image.url) %>
The image exists.
<% else %>
The image is not here.
<% end %>
And it always prints "the image is not here" when I load the page. Directly below I am displaying my image using image_tag, so I know for fact that the image is there.
I've also tried
<% if File.exist?(Rails.root + #style.style_image.url) %>
with no luck. I also tried using FileTest.exist?, FileTest.exists?, and File.exists? but none will tell me true when the image is definitely there.
Is there something I'm missing? Any guidance would be appreciated very much. I'm only a few months into Ruby and Rails so I'm probably missing something dumb.

I think you want #style.style_image.path instead of .url.
Print it out to be sure.

Any time you are dealing with web URLs and files on disk, you have to be careful which "path" you are using, and keep it straight whether you are telling your code to where to find the file, or a browser how to request that same file.
The web server hides the OS's file structure from the user/browser for lots of reasons, with security being a very high one. For instance, URLs are relative to the server, not the root of the drive.
What you ran into is very common and odds are really good that everyone who is working with programming for the web has, or will, run into the same thing, probably several times.

Related

Can I access packages within embedded ruby?

I made an XML configuration file that contains information to be accessed from both my Ruby script files and my HTML files. For Ruby purposes, this file contains things like links and admin authentication credentials that the script will need to pull from my database (I know this isn't safe--that's not an issue for me right now). For the HTML, it contains titles and other graphical information for the widgets where I'll be displaying this received information.
I was able to access the XML file in Ruby with the following:
require 'xmlconfigfile'
parent_directory = File.expand_path(".")
config = XmlConfigFile.new(parent_directory + '/configuration_file.xml')
info_i_need = config["/config/path_to_info/the_info"]
No problems there. Now, in the HTML, each widget is assigned a name, and I'd like to replace those names with strings that are located in this XML file. So I tried using embedded Ruby:
<div data-title=<% XmlConfigFile.new(File.expand_path(".") + '/configuration_file.xml')["/config/path_to_info/the_info"] %> ></div>
I didn't really expect this to work. I'm not sure if you can/should access Ruby packages in this way, but I can't find any other way to get the information I need into the HTML.
I really appreciate any corrections to this code or suggestions to alternate approaches. Thanks!

Is there a template for a website that accepts an uploaded file, does something, and lets the user download the result?

I have a few Ruby scripts that process text files in different ways, that many of my friends find useful. However, most of the people I know are not comfortable running scripts on the command line. The easiest thing for them would be to create a simple webpage where people could upload a file, select a few options, have it processed, and then download the result.
I know it wouldn't be too hard to build something like this in Rails or Merb or something like that, however it seems like a very common problem, so I was wondering if there was already some kind of template, or similar application that I could easily modify, i.e. let the user upload a file, choose a few options, then {fill in code to do something with file}, let the user download the resulting file?
In the past I used Carrierwave to upload user avatars.
If you are used to Rails it's really straightforward.
Let it be a TextFile resource:
gem 'carrierwave'
$ rails g scaffold textfile content:string title:string etc etc
$ rails g uploader textfile
class TextFile < ActiveRecord::Base
attr_accesible :content
mount_uploader :content, TextFileUploader
end
And that is is pretty much all you have to do to obtain the app's skeleton. However, to answer your real question, no, I don't think there is already a rails app that does exactly that.
https://github.com/jnicklas/carrierwave
I found sinatra-fileupload, which pretty perfectly answers my question. It's a very minimalistic framework which works perfectly, I can just plug in the file handling, and change the layout etc a bit. There were many examples of sophisticated Rails plugins linked to databases, with versioning and stuff, but I really wanted the most minimal example.

strange issue with embedded ruby tags

i have started using rails 3.1.3. After running the first sample application, I saw all my ruby tags in html.erb files are strangely converted into tag.
for e.g.
<%= render :partial=> "abc.html" %> is converting to
<code> render :partial => "abc.html" %><code> in html output...
This seems very strange to me.. Am i missing something or what?
Maybe you have to name your file the right thing? I think the new standard is something like filename.html.erb.

How should I setup assets in Rails 3.1 to be able to show images that're created on fly?

I've Rails 3.1 application which generates some images in 'public/scene/ticket_123/*.png' on fly. It works normally in development mode, but in production all assets should be precompiled. So I can't use files that I've generated after application started.
Setting config.assets.compile = true hasn't solve my problem. Situation is only worse since ticket number changes - so images are in different directories which are continiously created on fly too.
How should I setup assets to be able to show images that're created after an application was started?
I had the same problem. I only found a work around by copying all my images into "public/images" and changed all the links to the new path.
That worked for me for the moment. I wait until somebody comes up with a better idea.
I hope that helps.
If found solution.
# In view I wrote
<img src=<%= mycontroller_image_get_path :filename=>file_name %> >
# In controller I created GET action
def image_get
send_file params[:filename], :disposition => 'inline', :type => 'image/png'
end
But you should care that file you're trying to send is in "#{Rails.root}/public" directory otherwise send_file says it can't found the file. (May be it is not necessary in /public but in Rails.root anyway). To change this behavior it can be useful to read this topic Can I use send_file to send a file on a drive other than the Rails.root drive?

PDFkit rails3.1 and development env

My Rails 3.1 app is using PDFkit to render specific pages, and I'm running into (what seems like) a common problem with where trying to generate the pdf is causing the process to hang.
I found this solution here on stackoverflow: rails 3 and PDFkit. Where I add a config.threadsafe! entry in my development.rb file and this works BUT it requires that for every change anywhere in the app I have to stop and restart my server to see my changes. NOT acceptable from a workflow - I'm currently setting up the styling for the PDF pages, and it's painfully slow process having to do this.
I also found the same issue reported here: https://github.com/jdpace/PDFKit/issues/110, and the issue points to this workaround: http://jguimont.com/post/2627758108/pdfkit-and-its-middleware-on-heroku.
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}
This removes the need to restart the change, BUT now when I load the pdf it's without the styles rendered from the asset pipeline because it's taking the assets from the public directory. I think I could work with this solution if I could know how to create the stylesheets for the pdf templates in the public folder. IS anyone developing with PDFKit and Rails3.1 where this is all working in sync?
Any help would be greatly appreciated!
Thanks!
Tony
Here is the setup I am using:
I run a second instance of rails server with rails server -p 3001 -e test which will handle my assets for the PDF. The server will print the assets requests as they come in, so I can check that everything works as expected.
I use the following asset_host in my config/environments/development file:
config.action_controller.asset_host = ->(source, request = nil){
"http://localhost:3001" if request && request.env['REQUEST_PATH'].include?(".pdf")
}
If you are using Pow, you can use multiple workers. Add this to your ~/.powconfig
export POW_WORKERS=3
(taken from Pow manual)
There's a problem with pdfkit in Rails 3.1. See my answer to this related question:
pdfkit does not style pdfs

Resources