Test environment - errors with Paperclip - ruby-on-rails

I am beginning to write tests for an application and I'm having trouble getting the test environment to work.
My system has Paperclip for file uploads and it's tripping up at any attempt to start the server or even to initialize a new company record! An example:
# Company.rb line 3
has_attached_file :photo,
:styles => {:medium => "200x300>", :thumb => "100x150>" },
:storage => :s3,
:s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
:path => "/photos/:style/:id/:basename.:extension"
# console
ruby-1.9.2-p0 :001 > Company.new
NoMethodError: 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.[]=
from /Users/san/Documents/san/app/vendor/plugins/paperclip/lib/paperclip.rb:232:in 'has_attached_file'
from /Users/san/Documents/san/app/app/models/company.rb:3:in '<class:company>'
from /Users/san/Documents/san/app/app/models/company.rb:1:in '<top (required)>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:454:in 'load'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:454:in 'block in load_file'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:591:in 'new_constants_in'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:453:in 'load_file'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:340:in 'require_or_load'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:491:in 'load_missing_constant'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:183:in 'block in const_missing'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:181:in 'each'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:181:in 'const_missing'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in 'const_missing_from_s3_library'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in 'const_missing'
from (irb):1
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:44:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:8:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands.rb:23:in '<top (required)>'
from script/rails:6:in 'require'
Do you know what's causing this conflict and how I can fix it?
I read through the Paperclip docs and I discovered the need to add:
require 'paperclip/matchers'
Spec::Runner.configure do |config|
config.include Paperclip::Shoulda::Matchers
end
to spec_helper.rb. Still getting the same error!

I reckon what that is is the s3.yml doesn't contain a key for your current Rails environment, probably test? Add one. That should fix it!

In the end the error was fixed by downgrading Paperclip to 2.3.6, which is what I was using up until a week ago. Hopefully this is helpful to anyone experiencing this error in the future!

Related

Inflection acronym not working well

I have a problem using inflections.acronym in my project because they have almost the same name
Basically, I have 2 Controllers: SEController and TSEController. They are completelly different.
I added the following lines im my inflections:
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'SE'
inflect.acronym 'TSE'
end
When I try to access my SEController it works fine, but when I try to acces my TSEController it says:
Loading development environment (Rails 3.2.11)
irb(main):001:0> SEController
=> SEController
irb(main):002:0> TSEController
NameError: uninitialized constant TSEController
from (irb):2
from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
If I remove the following line,
inflect.acronym 'SE'
my TSEController works fine
Loading development environment (Rails 3.2.11)
irb(main):001:0> TSEController
=> TSEController
What can I do about it?

NoSuchKey error for an existing object

I have my S3 account set up in Rails 3 like so:
AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com"
#Set up AWS S3
AWS::S3::Base.establish_connection!(
:access_key_id => 'itsa',
:secret_access_key => 'secret'
)
BUCKET='wetten'
But for some reason it says that my files do not exist. I have given 'Everyone' Open/Download permissions on the files in S3.
In the Rails console, for this file:
>> AWS::S3::S3Object.exists?('/xml/BWBR0001821.xml', 'wetten')
true
>> AWS::S3::S3Object.find('/xml/BWBR0001821.xml', 'wetten')
AWS::S3::NoSuchKey: No such key `/xml/BWBR0001821.xml'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/aws-s3-0.6.3/lib/aws/s3/object.rb:177:in `find'
from (irb):11
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from C:/Users/Maarten/RubymineProjects/wetten-api/script/rails:6:in `require'
from C:/Users/Maarten/RubymineProjects/wetten-api/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
What's wrong?
UPDATE: I noticed that in object.rb, the AWS S3 gem (v0.6.3) does the following:
bucket = Bucket.find(bucket_name(bucket), :marker => key.previous, :max_keys => 1)
where key is '/xml/BWBR0001821.xml', and key.previous is '/xml/BWBR0001821.xmk'... What the hell? This seems like a bug to me.
Found the error. Apparently, the find method gets confused when by the first forward slash. Using xml/BWBR0001821.xml as a file name works:
>> AWS::S3::S3Object.find('xml/BWBR0001821.xml', 'wetten')
#<AWS::S3::S3Object:0x49387284 '/wetten/xml/BWBR0001821.xml'>

Twitter's activerecord-reputation-system gem + Devise

I am working with Twitter's new activerecord-reputation-system gem, yet it doesn't seem to be registering in my app.
I ran to install (docs have a typo):
gem install activerecord-reputation-system && rails generate reputation_system && rake db:migrate
I've included it in my gemfile and tried pulling directly from github, as well as restarting my local server. The error is the following:
ActionController::RoutingError (undefined method has_reputation' for #<Class:0x007fa7ed783ec0>):
app/models/post.rb:18:in'
app/models/post.rb:1:in <top (required)>'
app/controllers/posts_controller.rb:1:in'
when I try to add votes to posts with this code:
class Post < ActiveRecord::Base
belongs_to :user
has_reputation :votes,
:source => :user,
:aggregated_by => :sum
end
When I try to add votes to users with the same code I get:
undefined method `has_reputation' for #<Class:0x007fa7efb70388>
app/models/user.rb:17:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
config/routes.rb:4:in `block in <top (required)>'
config/routes.rb:1:in `<top (required)>'
This error occurred while loading the following files:
/Users/username/appname/config/routes.rb
routes.rb, Line 4:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
Any ideas?
You should require reputation_system in your app. You can do this by adding to your Gemfile:
gem 'activerecord-reputation-system', :require => 'reputation_system'
Update: As of version 2.0.0 you don't need to require reputation_system anymore:
gem 'activerecord-reputation-system'

Rails, Omniauth, Authlogic, and Facebook - first step

I'm trying to get Facebook logins working on my app using Rails, Authlogic, and Omniauth. I'm following the first steps of http://railscasts.com/episodes/235-omniauth-part-1 as precisely as possible.
I have:
added the omniauth gem, run bundle install.
created a new Authentication nifty-scaffold with fields user_id, provider, and uid.
added the route: match '/auth/:provider/callback' => 'authentications#create'
added to config/initializers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, '20...my_app_id', 'bc...my_app_secret'
end
The server then fails to restart:
/Users/san/.rvm/gems/ruby-1.9.2-p0/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:
in 'const_missing_from_s3_library': uninitialized constant
OmniAuth::Strategies::Facebook (NameError)
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in 'const_missing'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:22:in 'const_get'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:22:in 'provider'
from /Users/san/Documents/sanj/myapp/config/initializers/omniauth.rb:2:in 'block in <top (required)>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'instance_eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'initialize'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:7:in 'initialize'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:33:in 'new'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:33:in 'build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'block in build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'each'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'inject'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:162:in 'app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application/finisher.rb:35:in 'block in <module:Finisher>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:25:in 'instance_exec'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:25:in 'run'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:50:in 'block in run_initializers'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:49:in 'each'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:49:in 'run_initializers'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:134:in 'initialize!'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:77:in 'method_missing'
from /Users/san/Documents/sanj/myapp/config/environment.rb:5:in '<top (required)>'
from /Users/san/Documents/sanj/myapp/config.ru:3:in 'block in <main>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'instance_eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'initialize'
from /Users/san/Documents/sanj/myapp/config.ru:1:in 'new'
from /Users/san/Documents/sanj/myapp/config.ru:1:in '<main>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:35:in 'eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:35:in 'parse_file'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:162:in 'app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:253:in 'wrapped_app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:204:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands/server.rb:65:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:30:in 'block in <top (required)>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in 'tap'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in '<top (required)>'
from script/rails:7:in 'require'
from script/rails:7:in '<main>'
I had to do 'require "omniauth"' or 'require "omniauth-facebook"' in my config/development.rb file.
The omniauth gem was just bumped to version 1.0 as of yesterday (http://intridea.com/2011/11/2/omniauth-1-0), and there are some changes to how the gem is used that are not covered in the railscast.
I found my solution on this page: https://github.com/intridea/omniauth/wiki/OmniAuth-1.0
The biggest different is that each strategy is now contained within its own gem, so for facebook, you would change the line in your gem file that currently reads "gem 'omniauth'" to "gem 'omniauth-facebook'".
I didn't have to change any other code, and you may have to run "bundle update" to get everything to click through.
I just ran into this problem and this is what was wrong with me:
You can't have your facebook oauth gem in the Development list in your gem file. Heroku only reads the Production ones, so move it up out of the Development list.

Rails ActiveSupport Issue with state gems for notifications

I have installed multiple state_machine gems to my app to use them for a notification system but every time I run into an ActiveSupport issue. It usually looks something almost identical to this:
>> m = Message.new
TypeError: wrong argument type nil (expected Module)
from /home/Ryan/appname/app/models/message.rb:2:in `include'
from /home/Ryan/appname/app/models/message.rb:2
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:406:in `load_without_new_constant_marking'
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:406:in `load_file'
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:547:in `new_constants_in'
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:405:in `load_file'
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:285:in `require_or_load'
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:451:in `load_missing_constant'
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:106:in `rake_original_const_missing'
from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2503:in `const_m
issing'
from /home/Ryan/.bundle/ruby/1.8/gems/activesupport-2.3.9/lib/active_sup
port/dependencies.rb:118:in `const_missing'
from (irb):2
I'm on a windows 7 machine using activerecord, bundler to install the gems, and rails 2.3.9..where am I going wrong? Is there some modification to the database I need to be making?
Edit: message.rb
include AlterEgo # include this first
include AlterEgo::ActiveRecordAdapter
state :unread, :default => true do
handle :state do
"unread"
end
transition :to => :read, :on => :view!
end
state :read do
handle :state do
"read"
end
end
I'm not positive, but I am having trouble adapting this for activerecord. do i need to create a new database?
EDIT
I ended up just using oldschool acts_as_state_machine since this is such a simple implementation

Resources