Paperclip & Fog: Getting 'NameError (uninitialized constant Fog):' - ruby

Paperclip/Fog works on my local machine (Mac 10.8.4), but fails on server (Ubuntu 10.04 LTS). We have been using paperclip fine with local storage, but last night, migrated to cloud files and I'm getting this: 'uninitialized constant Fog' error.
Console Output:
Started PUT "/projects/car-tournament-of-champions-catoc" for 70.112.118.118 at 2013-08-19 06:44:57 +0000
Processing by ProjectsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"dwYOstjhuj2npLioC2rcXBKQ4lhGMlLt8s+epBm5vzk=", "project"=>{"name"=>"Car Tournament of Champions™ (CATOC™)", "headline"=>"Introducing true audiophile sound to the automotive environment", "about"=>"Car Audio Tournament of Champions (CATOC) is the world leader in premium sound system evaluation and marketing", "project_image"=>#<ActionDispatch::Http::UploadedFile:0x000000064a96c8 #original_filename="CATOC_MAIN_K.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"project[project_image]\"; filename=\"CATOC_MAIN_K.jpg\"\r\nContent-Type: image/jpeg\r\n", #tempfile=#<File:/tmp/RackMultipart20130819-4418-97i2l5>>, "category_id"=>"1", "paypal_email"=>"pre-registration#catoc-cca.com", "extra_amount"=>"0", "extend_days"=>"0"}, "button"=>"", "id"=>"car-tournament-of-champions-catoc"}
Completed 500 Internal Server Error in 12ms
NameError (uninitialized constant Fog):
app/controllers/projects_controller.rb:387:in `block in update'
app/controllers/projects_controller.rb:386:in `update'
Gemfile:
gem 'rails', '3.2.3' #ruby 1.9.3-p194
gem 'fog'
gem 'paperclip', '~> 3.0'
application.rb file:
config.paperclip_defaults = {
:path => "images/:class/:id/:attachment/:style/img_:fingerprint",
:storage => :fog,
:fog_credentials => {
:provider => 'Rackspace',
:rackspace_username => '<rackspace userid>',
:rackspace_api_key => '<rackspace api key>',
:region => 'dfw',
:persistent => false
},
:fog_directory => "prod_image_container",
:fog_public => true,
:fog_host => "http://c8d0f182112c0c3e585e-d37a976c417d5d9c71ba0df711c60fa4.r4.cf1.rackcdn.com"
}
project.rb file:
class Project < ActiveRecord::Base
attr_accessible :project_image
has_attached_file :project_image, :styles => {:medium => "260x180#"}
end

Can you verify that you have installed the fog gem as well as the version?
You should be able to do this by executing bundle show.

Related

bundle exec rake snorby:setup - error rake aborted! invalid hash

While executing below command to setup the snorby on ruby on rails, i get the error: rake aborted! invalid hash. Pls help
#bundle exec rake snorby:setup
#Jammit Warning: Asset compression disabled -- Java unavailable.
No time_zone specified in snorby_config.yml; detected time_zone: Asia/Kolkata
750d6d911891ab576bdbc6b1c4dc5ecf73cb95fd145ab7b16194636094daa3b4ba42b01c05d991c8c174919162e00152a129c645b1e0508850042cf224f165af
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1007 (HY000) at line 1: Can't create database 'snorby'; database exists
[datamapper] Finished auto_upgrade! for :default repository 'snorby'
rake aborted!
invalid hash
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Below are the versions currently used:
#ruby 1.9.3p551 (2014-11-13 revision 48406) [x86_64-linux]
#Bundler version 1.9.3
#Ubuntu 20.04 LTS (focal)
Modifying the entries in seeds.rb file as given below, the error has been resolved. In the seeds.rb changed the line for Default user setup from
User.create(:name => 'Administrator', :email => 'snorby#snorby.org', :password => 'snorby', :password_confirmation => 'snorby', :admin => true) if User.all.blank?
To
User.create(:name => 'Administrator', :email => 'snorby#snorby.org', :encrypted_password => 'snorby', :password_confirmation => 'snorby', :admin => true) if User.all.blank?
i.e the :password was changed to :encrypted_password

Set cookie expiration time in Ruby

I am using Ruby with Sinatra do develop a web application.
I have Ruby version 2.3.0, Sinatra 5.0.30
Following the suggestions from Rails cookies, set start date and expire date, I tried this:
#language = 'en-US'
response.set_cookie(:USER_LANGUAGE, :value => #language, :expires => 1.hour.from_now, :domain => '.example.com')
At first I thought it worked because the cookie set except the expiration time is still just only for the session. The error in my Apache error log says this:
NoMethodError - undefined method 'hour' for 1:Fixnum:
Please note: none of these worked to resolve the problem (none of them could be properly found by the compiler)
require 'active_support'
require 'active_support/all'
require 'activesupport'
So, I tried this instead:
#language = 'en-US'
response.set_cookie(:USER_LANGUAGE, :value => #language, :expires => 30, :domain => '.example.com')
Just to see what would happen and nothing changed, it still only expires with the session.
How should I go about setting an expiration time for my cookies in Ruby with Sinatra?
Sinatra doesn't have the ActiveSupport library which provides a helper for number-to-time, so 1.hour.from_now doesn't works here.
You should use this:
class SinatraApp < Sinatra::Base
use Rack::Session::Cookie, :key => 'rack.session',
:domain => 'foo.com',
:path => '/',
:expire_after => 2592000, # In seconds
:secret => 'some_secret'
And set a time in seconds. Because the Sinatra session comes from Rack::Session.
HOW TO ENABLE SESSIONS WITH SINATRA

Nothing happened when i run db:seed on Heroku

My app is made by Sinatra any everything working fine on my local server.
But when i deployed to Heroku and i ran this command
$ heroku run rake db:seed
Nothing happened even this command worked fine.
$ heroku run rake db:migrate
So this is my seed code
require_relative '../app/models/question'
require_relative '../app/models/answer'
require_relative '../app/models/user'
require 'faker'
class TaskSeed
def self.faker
20.times do
Question.create(
:content => Faker::Lorem.sentence(200, true),
:headline => Faker::Lorem.sentence,
:user_id => Faker::Number.between(1, 10)
)
end
40.times do
Answer.create(
:content => Faker::Lorem.sentence(20, true),
:headline => Faker::Lorem.sentence,
:user_id => Faker::Number.between(1, 10),
:question_id => Faker::Number.between(1, 10)
)
end
20.times do
User.create(
:username => Faker::Internet.user_name,
:email => Faker::Internet.email,
:encrypted_password => Faker::Internet.password(10, 20)
)
end
vote_type_arr = ['up','down']
50.times do
QuestionVote.create(
:type => vote_type_arr[rand(0..1)],
:question_id => Faker::Number.between(1, 20)
)
end
end
end
TaskSeed.faker
Anyone know how to fix this?
Thanks!
i am using PostgreSQL as my local since the project was made.
If your local database is clean(no testing data), then you could try heroku db:push
If you are using the default PostgreSQL, try
heroku pg:reset
heroku rake db:seed

papercrop wont crop with devise and rails 4

I'm trying to combine devise, papercrop, paperclip so that a user can crop and update an avatar. The images updates perfectly to the user but it won't crop. I pass it through the crop action and view. But it won't crop it, or display it anyway.
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_attached_file :avatar, :styles => {:thumb => '50x50', :medium => '400x300'},
:default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
crop_attached_file :avatar, :aspect => "16:9"
end
Registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def new
super # no customization, simply call the devise implementation
end
def update
if params[resource_name][:avatar].blank?
super
elsif params[:user][:email].blank? && params[:user][:password].blank? && params[:user][:password_confirmation].blank? && params[:user][:current_password].blank?
flash[:notice] = "Successfully updated user."
redirect_to #user
else
#user = User.find(current_user.id)
respond_to do |format|
if resource.update_attributes(params[resource_name].permit(:avatar))
flash[:notice]='Avatar successfully uploaded.'
format.html {
render :action => 'crop'
}
format.xml { head :ok }
else
format.html { render :action => "editpicture" }
format.xml { render :xml => #demotivator.errors, :status => :unprocessable_entity }
end
end
end
end
end
GEMFILE
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.1'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
gem 'bootstrap-sass', '~> 3.1.1'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
gem 'simple_form'
gem "font-awesome-rails"
gem 'devise'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
#/usr/local/bin/convert
gem "paperclip", "~> 4.1"
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'papercrop'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :production do
gem 'rails_12factor'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
Routes
Sportmatch::Application.routes.draw do
devise_for :users, :controllers => { :registrations => 'users/registrations' }
get 'users/:id' => 'users#show', as: 'user'
get "pages/home"
get "pages/main"
devise_scope :user do
patch '/users/:id' => 'users/registrations#update'
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'pages#home'
user view were image is displayed
<%= image_tag #user.avatar.url(:medium) %>
Trace
Started PUT "/users" for 127.0.0.1 at 2014-03-03 19:15:39 +0100
Processing by Users::RegistrationsController#update as HTML
Parameters: {"utf8"=>"â", "authenticity_token"=>"IpUoltuKU4yPbcdH1HSikaEyBsYVoJS+d1G94FZWnbc=", "user"=>{"email"=>"gjores#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007ffed1e90d68 #tempfile=#<File:/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/RackMultipart20140303-26872-1cc4n5d>, #original_filename="DSC_0803.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"DSC_0803.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Update"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
Unpermitted parameters: email, password, password_confirmation, current_password
(0.1ms) BEGIN
Command :: file -b --mime-type '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/4e6496f0d2ff11c4404858ce223f6cfa20140303-26872-d6ddec'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]'
Command :: convert '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' -auto-orient -resize "50x50" '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-1jmbx70'
Command :: file -b --mime '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-1jmbx70'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]'
Command :: convert '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' -auto-orient -resize "400x400" '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-hnooz4'
Command :: file -b --mime '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-hnooz4'
Command :: file -b --mime-type '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/739ef719a4c172dcd3daed74ad65998420140303-26872-1f6m43w'
SQL (3.3ms) UPDATE "users" SET "avatar_file_name" = $1, "avatar_file_size" = $2, "avatar_updated_at" = $3, "updated_at" = $4 WHERE "users"."id" = 2 [["avatar_file_name", "DSC_0803.jpg"], ["avatar_file_size", 1865679], ["avatar_updated_at", Mon, 03 Mar 2014 18:15:39 UTC +00:00], ["updated_at", Mon, 03 Mar 2014 18:15:40 UTC +00:00]]
[paperclip] deleting /Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/original/DSC_1046.jpg
[paperclip] deleting /Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/thumb/DSC_1046.jpg
[paperclip] deleting /Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/medium/DSC_1046.jpg
(4.3ms) COMMIT
Command :: identify -format '%wx%h,%[exif:orientation]' '/Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/original/DSC_0803.jpg[0]' 2>/dev/null
Rendered devise/registrations/crop.html.erb within layouts/application (152.1ms)
Rendered shared/_flash_messages.html.erb (0.1ms)
Completed 200 OK in 1260ms (Views: 161.5ms | ActiveRecord: 8.5ms)
Started PATCH "/users/2" for 127.0.0.1 at 2014-03-03 19:15:51 +0100
Processing by Users::RegistrationsController#update as HTML
Parameters: {"utf8"=>"â", "authenticity_token"=>"IpUoltuKU4yPbcdH1HSikaEyBsYVoJS+d1G94FZWnbc=", "user"=>{"avatar_original_w"=>"2000.0", "avatar_original_h"=>"3008.0", "avatar_box_w"=>"400", "avatar_crop_x"=>"140", "avatar_crop_y"=>"230", "avatar_crop_w"=>"1647", "avatar_crop_h"=>"1235", "avatar_aspect"=>"1.3333333333333333"}, "commit"=>"Save", "id"=>"2"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
Rendered devise/registrations/edit.html.erb within layouts/application (4.9ms)
Rendered shared/_flash_messages.html.erb (0.1ms)
Completed 200 OK in 84ms (Views: 12.4ms | ActiveRecord: 1.3ms)
Papercrop does not work with the Turbolinks gem. Disable Turbolinks on the page by adding "data-no-turbolink" to the <body> tag.

sending file with pony by sinatra app - missing file

I want to send a email from my sinatra application.
Here is the code:
require 'pony'
class Cms < Application
get "/mail" do
Pony.mail :to => 'to#gmail.com',
:from => "from#gmail.com",
:subject => "Thanks for signing my guestbook!",
:via => :sendmail,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:user_name => 'user#gmail.com',
:pass => 'pass',
:enable_starttls_auto => false
},
:body => erb(:"cms/mail")
redirect '/'
end
end`
Thin is starting application with no errors, but When i request myapp.local/mail i've got an error:
LoadError - no such file to load -- mail/network/delivery_methods/smtp:
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/configuration.rb:31:in lookup_delivery_method'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/configuration.rb:25:in delivery_method'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:111:in delivery_method'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/message.rb:116:in initialize'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:50:in new'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:50:in new'
/var/lib/gems/1.8/gems/pony-1.4/lib/pony.rb:174:in build_mail'
/var/lib/gems/1.8/gems/pony-1.4/lib/pony.rb:138:in mail'
./app/controllers/cms.rb:8:in GET /mail'
File /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb exists.
I was getting this same error when I was using the inline configuration of the Mail gem:
mail.delivery_method :sendmail
mail.deliver!
Removing that first line, and moving the configuration to immediately following the loading of the mail gem fixed it.
Wherever in your app you require 'mail' just configure it immediately:
require 'mail'
Mail.defaults do
delivery_method :sendmail
end
Update: This worked for awhile... But then for some reason I began seeing this error:
rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/mail-2.5.4/lib/mail/fields/common/common_address.rb:9:in `parse': no such file to load -- mail/elements/address_list (LoadError)
Update2: The failures happen randomly it seems. Something about the way the autoload works in Ruby 1.8.7-p374 is causing it to not be able to find files that do in fact exist. Also, I am using slimgems not rubygems.
These are the hacks I've had to implement so far to use Mail with multi-part email and sendmail delivery method:
require 'mail'
require 'mail/network/delivery_methods/sendmail'
require 'mail/elements/address_list'
require 'mail/fields/common/common_address'
require 'mail/elements/content_type_element'
require 'mail/elements/address'
require 'mail/elements/content_transfer_encoding_element'
Mail.defaults do
delivery_method :sendmail
end

Resources