Paperclip generating wrong URLs in Heroku - heroku

Paperclip is generating wrong URLs in Heroku.
I have an Audio model which has a mp3 field as follows:
class Audio < ActiveRecord::Base
has_attached_file :mp3,
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:bucket => S3_CREDENTIALS[:bucket],
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
I am calling audio.mp3.url from a controller, and it returns
http://s3.amazonaws.com/MyApp/audios/mp3s//original/96a9ae89302fdf8462ee05eb829f2e17578b144e20120908-2-11f61zr.mp3?1347135050
instead of
http://s3.amazonaws.com/MyApp/audios/mp3s/000/000/004/original/96a9ae89302fdf8462ee05eb829f2e17578b144e20120908-2-11f61zr.mp3?1347135050
(which works)
Why is it missing the '000/000/004' part of the route?
The same model is generating the right URL when used in a view.
Any help?
I am using paperclip 3.2.0 and Rails 3.1.8.
Any help?

It looks like the '000/000/004' part of the route is the new to paperclip as of 3.0 feature of :id_partition. Try changing :id to :id_partition. Maybe you changed url in your code after already having uploaded something?

Related

Sinatra + Paperclip : undefined method `descendants'

I'm currently converting a very small Rails application to Sinatra. This Rails app was relying on ActiveRecord + Paperclip + Amazon S3 for picture storage, and I have troubles making it work in Sinatra (but I believe it would be the same for any kind of Rack-based application), with ActiveRecord + Paperclip + Amazon S3 as well.
Here's what I have so far:
Gemfile:
gem 'paperclip'
gem 'paperclip-rack', require: 'paperclip/rack'
gem 'aws-sdk'
Model:
class Photo < ActiveRecord::Base
include Paperclip::Glue
has_attached_file :image,
:storage => :s3,
:s3_credentials => "config/s3.yml",
:bucket => 'mybucket',
:path => ':style/:photo_id.:extension',
:styles => {
:original => '1200x1200>',
:miniature => '80x80#',
:slideshow => 'x200'
}
end
View:
form method="post" action="/photos/add" enctype='multipart/form-data'
input type="file" name="image"
input#submit-button type="submit"
Route/Action:
post '/photos/add' do
photo = Photo.new
photo.image = params[:image]
#image[:tempfile] = params[:image][:tempfile],
#image[:filename] = params[:image][:filename],
#image[:content_type] = params[:image][:type],
#image[:size] = params[:image][:tempfile].size
photo.save
redirect "/"
end
And the error I'm getting when trying to upload something :
NoMethodError at /admin/photos/add
undefined method `descendants' for Paperclip::Validators::AttachmentFileNameValidator:Class
file: attachment.rb location: each line: 393
I've tried to play with the :image param in my route (and manually assign each value to the field that was created by paperclip, see the commented lines) but it didn't seem to work any better. Any idea guys ? I'm stuck and have no clue where to start to get this to work.
Note: I've removed all validations and stuff so I even don't understand the error message I'm getting.
I (think) I was able to monkey patch the #descendants method using this SO answer:
Look up all descendants of a class in Ruby
So I monkey-patched Paperclip in one of my initializers, which seemed okay to me since I don't care about validating the content type of the attachment:
module Paperclip
module Validators
class AttachmentFileNameValidator
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end
class AttachmentContentTypeValidator
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end
class AttachmentFileTypeIgnoranceValidator
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end
end
end
I'm using Paperclip 4.2 and Sinatra 1.4.5
There is no official support for non-Rails apps using Paperclip. See a discussion on it here
You'll notice if you look in Paperclip::Validators::AttachmentFileNameValidator:Class you'll see:
class AttachmentFileNameValidator < ActiveModel::EachValidator
def initialize(options)
options[:allow_nil] = true unless options.has_key?(:allow_nil)
super
end
ActiveModel is bundled w/ rails, so I'm guessing it's blowing up on the inheritance in here. Either way, I'd move over to an uploader friendly to sinatra like carrierwave

Create a link to a nested resource where the parent does not have resource name in the URL

I have setup my routes to work without the resource in the url (i.e. /username/posts, as opposed to /users/username/posts) - using friendly_id gem inorder to use :username instead of :id.
The resources are under a namespace (cpanel) and work fine.
My routes are setup like this:
namespace :cpanel do
resources :users, :path => '', :constraints => { :id => /[\w+\-\_]+/ } do
resources :posts
end
end
Navigating to /cpanel/username/posts works fine, but I am having trouble setting up my link_to I am using the path cpanel_user_posts_path(#user) but this creates a link to the URL /cpanel/users/:username/apps.
How can I create a link to the path: /cpanel/:username/apps?
Thanks in advance.
Did you try something like this:
get 'cpanel/:username/apps', to: 'controller#action
You can always hard code it (as a last resort) via:
link_to "Apps", "/#{current_user.username}/apps"

Overriding Session Controller Prevents custom views being used

Devise (2.1) was using my custom views fine until I told it to use a custom controller. Now it ignores my custom views.
Previously everything worked fine:
Tell Devise to use custom views in /config/devise.rb
# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
config.scoped_views = true
Add custom view: /app/views/subscribers/session/new.html.erb
Set up routes in /config/routes.rb
devise_for :subscribers
Then I added a custom SubscriberSessionsController as /app/controllers/subscriber_session_controller.rb
class SubscriberSessionsController < Devise::SessionsController
before_filter :isInIframe
private
def isInIframe
#hide_navbar = session[:in_iframe]
end
end
And modified /config/routes.rb to tell Devise to use this new controller instead of its default:
devise_for :subscribers, :controllers => {
:sessions => "subscriber_sessions"
}
Once I restart my server, Devise now uses this controller but ignores my custom view.
As is so often the case, ten minutes after posting the question I cracked it.
The reason Devise wasn't finding the view was it was looking for it in a different folder.My replacement controller was called subscriber_sessions.rbso devise was no longer looking in views/subscribers/sessions but views/subscribers/subscriber_sessions.
I solved this problem with the following:
Changed my subscriber routes to:
devise_for :subscribers, :controllers => {
:sessions => "subscribers/sessions"
}
Renamed my subscriber_sessions controller to just sessions and moved it into a subscribers folder so its new name & location are: app/controllers/subscribers/sessions_controller.rb
I also had to add a namespace to the class so the new sessions_controller.rb file looks like this"
class Subscribers::SessionsController < Devise::SessionsController
before_filter :isInIframe
private
def isInIframe
#hide_navbar = session[:in_iframe]
end
end

Paperclip not saving attachment on Padrino

I am building a site using padrino. I use paperclip for uploading logos to one of the models. The problem I am experiencing is that Paperclip does not save the attachment, but also does not throw any errors. I think that the parameters passed to the controller are of incorrect type, as the params[:logo] is a hash and should probably be a some kind of a file type? How can I make Paperclip save the attachments passed in the parameters?
The model:
class Charity < ActiveRecord::Base
include Paperclip::Glue
attr_accessible :name, :description, :logo
has_attached_file :logo, path: "/public/:attachment/:id/:basename.:extension"
end
The logo is set in the controller like so:
post :create do
#charity = Charity.new(params[:charity])
if #charity.save!
flash[:notice] = 'Charity was successfully created.'
redirect url(:charities, :edit, id: #charity.id)
else
render 'charities/new'
end
end
The form passing the parameters to the controller looks like this (parts omitted for brevity):
- form_for :charity, url(:charities, :create), multipart: true, class: :form do |f|
(...)
.group
==f.label :logo
==f.error_message_on :logo
==f.file_field :logo
(...)
I am using Paperclip 2.7.0 and Padrino 0.10.7.
I also have added this to boot.rb as per Using Paperclip with Padrino :
Padrino.before_load do
File.send(:include, Paperclip::Upfile)
Paperclip.options[:logger] = Padrino.logger
Paperclip.options[:command_path] = "/usr/local/bin"
ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Paperclip::Schema)
ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema)
ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema)
end
Mmm, I've no idea. Maybe this:
path: Padrino.root('/public/:attachment/:id/:basename.:extension')
Can you go in padrino console and try:
>> require 'open-uri'
>> puts Paperclip.default_options
>> c = Charity.create!
>> c.logo = open('http://1.bp.blogspot.com/-0Hn3AjTJj6U/TZHe3ragXGI/AAAAAAAAA1M/_SBk3dx61EE/s1600/med_funny-cat.jpg')
>> c.save!

paperclip and rails 3.1 NoMethodError and nil object

I just updated Rails 3.0.10 to 3.1 and everything seems to be ok.
But Paperclip don't work anymore and give me an error :
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=
I'm unable to find the problem.. thank for help
[edit after comment]
Model :
class Community < ActiveRecord::Base
has_attached_file :commicon, :styles => { :thumb => "80x80>", :medium => "34x34!", :small => "20x20>", :rectangle => "80x40!", :mediumrect => "34x17>" , :smallrect => "20x10>"}
Controller:
class CommunitiesController < ApplicationController
def show
#community = Community.find(params[:id])
View :
<%= image_tag #community.commicon.url(:thumb) %>
I think nothing so special ?
I found my problem, thanks to the deprecated information of rails.
I updated my rails 3.1 to 3.2 and then it tell me that the plugins will be removed in the version 4.
After a quick look, I saw that I have a gem AND a plugin for paperclip and think my app was using the plugin.
Trashed the vendor folder and everything OK

Resources