Rails 3 Doesn't Look In Public Folder For Assets On Show Method - ruby-on-rails-3

In my show views whenever I try and display an image using the image_tag builder rails doesn't look for images in the public folder inside of my show views...
For instance:
<%= image_tag "thumbnails/fish.jpg" %>
Will produce this:
ActionController::RoutingError (No route matches [GET] "/uploads/thumbnails/fish.jpg"):
I'm using the paperclip Gem for my upload model and I'm saving uploads to a different folder than the public folder for security reasons, and yes, this show view does occur within the Upload controller...
In my Upload model I use this line to save uploads to a non-public folder:
has_attached_file :upload, :path => ":rails_root/:class/:id/:basename.:extension",
:url => ":rails_root/:class /:id/:basename.:extension"
Rake routes:
upload GET /uploads/:id(.:format) {:action=>"show", :controller=>"uploads"}
PUT /uploads/:id(.:format) {:action=>"update", :controller=>"uploads"}
DELETE /uploads/:id(.:format) {:action=>"destroy", :controller=>"uploads"}
/download/:id(.:format) {:controller=>"uploads", :action=>"download"}
Edit
Note: If I explicitly make an img tag and point the src to my image it works fine on my show views so I don't think it's a permission issue.

The answer was fairly simple, and I can't believe Rails is being this picky but I needed to include a forward slash at the beginning of the path as so:
"thumbnails/fish.jpg"
becomes
"/thumbnails/fish.jpg"
I'm still curious as to why this is only a problem on non-index views...

You may need to change this line to true:
config.serve_static_assets = false
in the environment/*.rb file (e.g. development.rb).
Or I have seen where this is a permissions issue on the directory in question so a CHMOD 777 on the directory may resolve it/point you in the right direction.
Here is an SO ticket on a similar issue

Related

Hanami routes helper don't work in templates

I have hanami application version 1.0.0
I have next routes.rb file:
get '/games', to: 'games#index'
root to: 'home#index'
I read docs and tried to use
<%= routes.games_path %>
in application.html.erb template, but recieved next error:
Hanami::Routing::InvalidRouteException: No route (path) could be generated for :games - please check given arguments
How can I use routers in hanami templates?
Instead of using resources, you can also add a name to your route:
get '/games', to: 'games#index', as: :games
Read more at: https://guides.hanamirb.org/routing/overview/
I added
resources :games, only: [:index]
to my routes.rb file and this fixed my problem.

Rendering CSS using PDFKit

I know that there have been other questions about this, but I've read through them all and haven't figured this out yet. I'm using the PDFKit gem to allow users to generate .pdfs of content on my site. I can render a pdf by adding .pdf to the url, but CSS doesn't transfer along with it. I understand from Googling that it's because of the asset pipeline, but I haven't figured out how to make it work.
I've installed both of teh necessary gems:
gem 'pdfkit', '~> 0.6.2'
gem 'wkhtmltopdf-binary'
In my application.rb file I added 'require pdfkit' at the top and the following to the application class:
config.middleware.use PDFKit::Middleware
config.cache_classes = true
config.eager_load = true
I then ran rake middleware
I also added an initializer called pdfkit.rb with teh following code that I found, in order to fix the .pdf from endlessly loading:
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
}
At this point adding .pdf to a url creates the pdf, but doesn't render the CSS at all. The README on pdfkit's github says:
"Resources aren't included in the PDF: Images, CSS, or JavaScript does not seem to be downloading correctly in the PDF. This is due to the fact that wkhtmltopdf does not know where to find those files. Make sure you are using absolute paths (start with forward slash) to your resources."
My CSS is called in the head as usual:
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
How do I make an absolute path? I don't think I just replace the stylesheet_tag with an ordinary
<link rel='stylesheet' href='bootstrap_and_customization.css.scss' type="text/css">

Getting routes.rb root to work in Rails

Started working with RoR again! I'm having trouble getting my config/routes.rb file to perform. I'm getting "uninitialized constant ApplicationsController" using RubyMine.
Here's what I have changed in my routes.rb after trying to search things down:
resources :applications
root :to => 'applications#index'
application_controller.rb has:
class ApplicationController < ActionController::Base
protect_from_forgery
end
I've ran my rake routes:
applications GET /applications(.:format) applications#index
POST /applications(.:format) applications#create
new_application GET /applications/new(.:format) applications#new
edit_application GET /applications/:id/edit(.:format) applications#edit
application GET /applications/:id(.:format) applications#show
PUT /applications/:id(.:format) applications#update
DELETE /applications/:id(.:format) applications#destroy
root / applications#index
rake rails:update and all changes were approved except altering routes.rb
Heres what its kicks out Rubymine side:
Started GET "/" for 127.0.0.1 at 2012-11-11 02:50:27 -0800
Connecting to database specified by database.yml
ActionController::RoutingError (uninitialized constant ApplicationsController):
Thanks for the help!
You should rename application_controller.rb to applications_controller.rb
Check if you have a file called applications_controller in your app/controller folder, or its name is application_controller, so rename it to applications_controller, if you don't have that file, then create your ApplicationsController:
rails generate controller applications

Troubles setting up Paperclip + AWS S3 for image storing in our Rails3/Heroku App

We have already built a rails app that has several users and an image for each of them. Doing all of the dev work on our localhost, we have working seeds for users & photos...but now that we are trying to use S3 for the image storage, we are running into errors during...always during the "seed" step of the migrations, when doing this:
rake db:migrate:reset
Apologies for the question, but we have have been banging our heads on this for 11 hours, having gone through every related Stack question on the subject. A lot of similar posts had a NoSuchBucket error and other types of issues, but we none of the suggested changes have fixed our issue...maybe it's related to the newest versions of the gems we are using?
We are using Rails 3.0.4, Ruby 1.8.7, Paperclip 2.3.8, aws-s3 0.6.2
We are adding seeds for initial users and a photo for each user using our seeds.rb file in the /migrate/ folder. This always worked fine when storing files and images on local machine (using paperclip, but not S3). We also have tested removing the seeds file and simply creating a new user with the working app and got the same error:
Credentials are not a path, file, or
hash
For the user module, we have tested both the option where we set the following S3 keys through both the (a) yml file and (b) directly in the user model.
access_key_id: 'secret'
secret_access_key: 'secret'
We have tried doing this from our localhost (not yet live on heroku), and we have also tried running this through Heroku.
We have tried seemingly every permutation of the layout of those keys, but the error we most frequently get is this:
can't convert Module into Hash
Googling this error message returns zero results, so we don't know what's happening there. This was the most frustrating part...seemingly every attempt got us back to this error.
We also tried both:
(1) hardcoding the access keys in the user model, both like this:
:access_key_id => ENV['accesskeyid'],
:secret_access_key => ENV['secretaccesskey'],
In this case, we often got this error:
You did not provide both required access keys. Please provide the
access_key_id and the secret_access_key.
Frustrating, because we always had both items listed, tested with and without quotes, changing up the order, etc.
We tried it both (a) with the ENV['accesskeyid'] and (b) without those...with simply
blahblah => 'accesskeyid'.
and (2) putting the keys into the yml file, like this:
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:photo/:filename"
with this in the yml file:
development:
access_key_id: accesskeyid
secret_access_key: secretaccesskey
bucket: ourbucketname
production:
access_key_id: accesskeyid
secret_access_key: secretaccesskey
bucket: ourbucketname
We tried this with single quotes around the keys, and without.
We also tried defining the bucket in the model, rather than in the yml file, and got the same error.
and (3), setting it up this way:
if Rails.env == "production"
S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "ourbucket"} else
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end
has_attached_file :photo,
:storage => :s3,
:styles => { :small => "50x50>", :thumb => "75x75>", :medium =>
"400x400>"},
:path => "/:photo/:filename"
With the same contents in our yml file.
This gave us this error:
credentials are not a file, path, or hash
Naturally, we quadruple-checked that we had the correct access keys (from our AWS account) and tested several different ways of setting up the hash, but never got what we wanted.
Here is the relevant portion of Gemfile:
gem 'aws-s3', :require => 'aws/s3' #For Storing Images on Amazon
gem 'paperclip'
As another attempt, we tried to use the gem right_aws, instead, in the Gemfile, but this resulted in this error:
no such file to load -- aws/s3 (You
may need to install the aws-s3 gem)
Note, we have been doing all of this and hitting all of these errors doing migrations from from localhost, not from the live Heroku app, but couldn't even get past this simple 'seed users' step.
Currently, our bucket is titled media.oururl.com. Is there some issue with having periods in the bucket name?
Going to ask the heroku guys about this, as well, but considering how amazing this community is, I am hoping one of you knows what we're doing wrong her.
MUCH appreciated - and hopefully this helps others who follow behind us.
excellent question. I spent quite some time with a similar issue a while ago
The primary issue is that you need to move the following code into it's own initializer file:
if Rails.env == "production"
S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "ourbucket"}
else
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end
Then, you should add the following line to your model where you have *has_attached_file :photo* The line to add is.
:s3_credentials => S3_CREDENTIALS,
This is what you were missing before.
Also, for when you declare your bucket name, make sure that is for standard us. If you use one of the other locations, you'll have to update the path appropriately.
Hope this helps!

Princely is generation text/html instead application/pdf?

I'm using Princely and Rails 3.
When I try to render a pdf like this..
respond_to do |format|
format.pdf do
render :pdf => "filename", :template => "path/template.pdf"
end
end
The downloaded file is a text/html format.
How can I fix this and make it work. I have already added the line bellow to config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
I had the same problem yesterday and came across this question in my search for a solution.
In my case, I had the Princely gem installed globally (well, in my global RVM repo) rather than anywhere in my project's directory structure. So none of the Princely code was ever being loaded. I ended up making a new "prince.rb" file in my project's conf/initializers with the following:
require 'princely'
require 'princely/rails'
At that point I still got rails errors because Princely 1.2.5/lib/princely/pdf_helper#stylesheet_file_path refers to ActionView::Helpers::AssetTageHelper, which is apparently a Rails 2ism. I just changed that method's body to "return stylesheet" and then everything worked.
I'm still fairly new to Rails; maybe someone that knows more can chime in with a better way.