Delayed_Paperclip job complete - ruby-on-rails-3

I am currently using delayed and it is up and running successfully. However, I can't seem to add a hook for when the jobs are completed from resque. The code I have currently is listening to the correct queue, but still seems to not work. I might be making a very simple, stupid mistake, but I can't seem to figure it out.
Here is the model (working fine as far as I can see):
class Entry < ActiveRecord::Base
attr_accessible :approved, :entry_text, :reviewed, :user_id, :photo
has_attached_file :photo, :styles => { :small => "250x250", :large => "500x500>" },
:url => "link-to-s3-will-be-env-var",
:path => "path/:id/:basename.:extension"
process_in_background :photo
end
And then my little worker class (the part that doesn't seem to be run)
class PhotoProcessor
#queue = :paperclip
def self.perform(photo_id)
puts "*****************OMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMG*********************"
puts photo_id.inspect
end
end
I can't get it to output the crazy string, just for testing purposes, but I can't even seem to have this method called. Thanks for any help, I am clearly missing something obvious.

Related

Delayed job and Mandrill: uninitialized constant Mandrill::API

I have mailer service where users can upload an .xls file with emails and some other user related data to send an email campaign.
I was having some timeout issues since it takes some seconds to process (as I do some validation and configuration for each email to be sent, eg: save records to database, check if an email was sent in the last 30 days, create personalised html code for each email (to create links that contain the email address as a parameter, etc).
After some research, moving this to a delayed job seemed reasonable, as suggested in this rails cast. The only problem is that I am having an error that says uninitialized constant Mandrill::API, it seems that when I run the job, the call require 'mandrill' doesn't work.
I created the task in my model file. Something like this
class Mailer < ActiveRecord::Base
attr_accessible :email, :lastname, :name
def self.send_mail(emails)
[...a lot of code here...]
require 'mandrill'
m = Mandrill::API.new ENV['MANDRILL_APIKEY']
message = {
:subject=> template.subject,
:from_name=> template.from_name,
:from_email=> from + "#" + email_domain,
:to=>mails,
:global_merge_vars=> [
{ :name => 'GREETING', :content => template.greeting},
{ :name => 'CONT1', :content => template.message},
{ :name => 'IMAGE', :content => image_url},
],
:html=>email_template,
:preserve_recipients => false,
:merge_vars => email_contents,
}
sending = m.messages.send message
end
end
from my controller I call Mailer.send_mails(emails) and it works just fine, but if I call Mailer.delay.send_mails(emails) I get the error. How can I fix this?
I have tried adding require 'mandrill' at the beginning of the class, before and after class Mailer < ActiveRecord::Base but nothing seems to work
Make sure to restart the delayed_job daemon to pick up any code changes. It does not auto-reload like the Rails development environment does.

Supplying data for a form re-population from a different model

so I have a tricky issue here I'm not sure how to solve.
I have a Provider model that has_many :educational_affiliations
EducationalAffiliation belongs_to :institution & belongs_to
:provider
I have about 9000 universities in my database, so I'm using the handy-dandy rails3-jquery-autocomplete gem to give me type-ahead support. That's all working great - on TOP of that I'm using cocoon to support the nesting of the :educational_affiliations form inside of the provider's edit form.
So here's where the issue comes — This works great for submitting new affiliation records, (I'm using some jquery to set the :institution_id on the :educational_affiliations_attributes object, works great)
BUT when I return to edit this later, of course the :institution_name isn't populated, because it's not part of the :educational_affiliations model, it's in the :institution model.
Here's what I just tried in my :educational_affiliations, which I assume is the right solution:
class EducationalAffiliation < ActiveRecord::Base
attr_accessible :degree, :graduation_year, :honors, :institution_name, :institution_id, :provider_id
belongs_to :institution
belongs_to :provider
# attr_accessor :institution_name
validates :institution_id, :graduation_year, :provider_id, :degree, presence: true
def institution_name
Institution.find(institution_id).name
end
end
(i had it working for saving using the attr_accessor, but I've commented it out for now)
So when I render the edit view with the above, I get a ActiveRecord::RecordNotFound: Couldn't find Institution without an ID error — but when I open a debugger on that statement, the model DOES seem to know the institution_id...so confused why it doesn't pick it up.
Am I just doing this in the worst way possible? I assume there's a dumb solution.. :)
Here's the partial that needs the name populated:
.nested-fields
= f.input :institution_name, url: autocomplete_institution_name_data_path, as: :autocomplete, :input_html => {:id_element => '#provider_educational_affiliations_institution_id'}
= f.input :institution_id, as: :hidden
= f.input :provider_id, as: :hidden, input_html: { value: current_provider.id }
= link_to_remove_association "Remove Degree", f
Instead of the virtual attribute method, try the following to define the attribute:
delegate :name, :name=, :to => :institute, :prefix => true

undefined method `stringify_keys'

When I try to upload image using Paperclip gem I got this error:
NoMethodError (undefined method `stringify_keys' for <ActionDispatch::Http::UploadedFile:0x000000025387f0>)
class MenuItem < ActiveRecord::Base
has_one :image
end
class Image < ActiveRecord::Base
belongs_to :menu_item
has_attached_file :image, :styles => {
:large => "640x480",
:medium => "300x300",
:thumb => "100x100"
}
end
I've seen this error happen before, usually when people attempt to call update_attributes like this:
update_attributes(params[:image])
The call should actually be this:
update_attributes(:image => params[:image])
A bit of a shot in the dark, but if that's it I'm sure we'll all be impressed.
After struggling for a while in rails 3.2.2 I managed to solve this in this manner
(image = Image.new(image: params[:image])).save
update_attributes(image: image)
I just had this problem, and to clarify things a bit, update_attributes is different from update_attribute.
The following should work:
update_attributes(:image => params[:image])
or
update_attribute(:image, params[:image])
There you go! There are other issues with update_attributes related to attr_accesible, but either works.

A better way to get paperclip thumbnail in index.html.erb

I've been struggling with this for hours. For some background, I have paperclip set up, keeping in mind that I may one day want to add multiple attachments. I followed Emerson's screencast to help me figure it out. (http://www.emersonlackey.com/article/paperclip-with-rails-3)
I now have this in my view, which shows what I want it to show. I had trouble for a long time because it was kicking up errors when a thumbnail didn't exist for some posts.
Anyway, wrote it, it's in my view, and I just think it's really ugly. I feel like I must be missing something. For one thing, I'm completely repeating myself on one line. Secondarily, I've got this code in my view. Is there something that I should be doing in my controller to help me keep my view cleaner?
Thanks a bunch!
<% if Asset.where(:piece_id => piece.id).first
my_asset = Asset.where(:piece_id => piece.id).first%>
<%= piece.id%>
<%= image_tag my_asset.asset.url(:thumb)%>
<% end%>
Because I haven't done anything to my controller to speak of, I'm leaving all of that code out. But here's what my models look like:
Assets
class Asset < ActiveRecord::Base
belongs_to :piece
has_attached_file :asset, :styles => {:large => ['700x700', :jpg], :medium => ['300x300>', :jpg], :thumb => ["100x100>", :jpg]}
end
Pieces
class Piece < ActiveRecord::Base
attr_accessible :assets_attributes,:name, :campaign_id,:election_date, :mail_date, :pdf_link, :photo_desc, :photo_stock, :killed, :format, :artist
belongs_to :client
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
validates :campaign_id, :presence => true
end
So your problem is that sometimes a Piece has a thumbnail and sometimes it doesn't, right?
I'd agree that your ERB solution smells bad. You could add a thumb_nail_url method to Piece:
def thumb_nail_url
asset = assets.first
asset ? asset.asset.url(:thumb) : nil
end
And then:
<% thumb_url = piece.thumb_nail_url %>
<% if thumb_url %>
<%= image_tag thumb_url %>
<% end %>
You could also wrap the above in a helper:
def piece_thumb_image_tag(piece)
thumb_url = piece.thumb_nail_url
thumb_url ? image_tag(thumb_url) : ''
end
and then:
<%= piece_thumb_image_tag piece %>

Rails 3 & paperclip - not rendering images

I've just started using Paperclip today and am having issues getting images to render. After some wrangling the photos are saving in the correct directory but there is a routing/rendering error:
ActionController::RoutingError (No route matches "/public/system/products/19/original/puppies-3.jpg")
However, the images are definitely saving in the correct directory. This is what's in my product model:
class Product < ActiveRecord::Base
validates :title, :presence => true
validates :description, :presence => true
validates :category, :presence => true
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
attr_accessible :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
attr_accessible :title, :description, :category, :price
has_attached_file :photo, :styles => { :small => "150x150>", :large => "400x400>" },
:path => ":rails_root/public/system/products/:id/:style/:basename.:extension",
:url => "/system/products/:id/:style/:basename.:extension"
end
This is in my view:
<%= image_tag #product.photo.url %>
At the moment it's simply returning the image basename instead of the image itself, any thoughts? Products is available as a resource in routes.rb, but do I need to explicitly make photos available also somehow? I'm also fairly new to Rails so struggling a little bit...
Are you sure that product with that id has a image attached? I think some of your products dont have images attached but u still looking for them. Maybe try to set a default_url. Something like this:
has_attached_file :photo, :default_url => "/images/missing.png"
This will show that image set in default_url for the products what dont have images attached.
It's ok, I've fixed it. I had left a field in my form which was naming the photo_file_name from when I couldn't get the files to save and was trying to manually override it to get it to work (face-palm). It's amazing how much clarity comes from a few hours sleep.