What happened to Mongoid.config.master.connection.host? - ruby-on-rails

I am trying to get Carrierwave (0.5.1) to work with Mongoid (2.0.0.beta.20), Rails 3. I followed every step at this guide.
In config/initializers/carrierwave.rb, I have:
CarrierWave.configure do |config|
config.grid_fs_database = Mongoid.database.name
config.grid_fs_host = Mongoid.config.master.connection.host
config.storage = :grid_fs
config.grid_fs_access_url = "/uploads"
end
When I try to start my server (rails server). In the console, I get:
...config/initializers/carrierwave.rb:3:in `block in <top
(required)>': undefined method `host' for #<Mongo::Connection:
0x00000103802420> (NoMethodError)
I don't understand why I am getting this error. I've looked everywhere
and can't seem to locate why this is happening...
It seems, Mongoid.config.master.connection.host doesn't work anymore
in newer versions of Mongoid. Was this removed? What is the
replacement for this?
So far my workaround is the following code:
CarrierWave.configure do |config|
config.grid_fs_database = Mongoid.database.name
config.grid_fs_host = 'localhost'
config.storage = :grid_fs
config.grid_fs_access_url = "/uploads"
end
Line 3, should be: config.grid_fs_host = 'localhost'. <-- Is there a better way to dynamically indicate the host depending on environment?

Found out that the mongo gem has changed. So it now has to be:
config.grid_fs_host = Mongoid.database.connection.primary_pool.host

Related

Kaminari params on first page config option

I am trying to set this option to true as described in the documentation. I used rails g kaminari:config which generated the following kaminari_config.rb :
Kaminari.configure do |config|
# config.default_per_page = 25
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
end
The params_on_first_page option not being present, I tried to add it before the end, this way : config.params_on_first_page = true
However, this doesn't seem to work, and I can't launch my rails server anymore. It gives me the following error :
Exiting
/home/vincent/workspace/bam-rails/config/initializers/kaminari_config.rb:10:in block in <top (required)>': undefined methodparam_on_first_page=' for #Kaminari::Configuration:0x0055c09deaddb0 (NoMethodError)
Did you mean? param_name=
I am using the version 0.17.0 of Kaminari. Am I missing something ?
Thank you in advance for your help.
Solved here : https://github.com/amatsuda/kaminari/issues/831
I missed to add github: "amatsuda/kaminari" in my Gemfile, whereas the option was only available on branch master.

`require': cannot load such file -- cancan (LoadError)

I'm using the CanCan gem with ActiveAdmin. It is working as expected in dev, but when pushed to a staging server I get the following error:
`require': cannot load such file -- cancan (LoadError)
In my case, this is caused by the 'require' line in ActiveAdmin's CanCan adapter.
I've searched Google for potential explanations but have come up empty-handed.
What might be the cause of such an error, and how can I go about fixing it?
Update:
Here is my config/initializers/active_admin.rb:
ActiveAdmin.setup do |config|
config.authentication_method = :authenticate_user!
config.authorization_adapter = ActiveAdmin::CanCanAdapter
config.cancan_ability_class = "Ability"
config.current_user_method = :current_user
config.logout_link_path = :destroy_user_session_path
config.allow_comments = false
config.batch_actions = true
end
Restarting the entire machine fixed this.
I had tried restarting both nginx and unicorn, but I had yet to restart the machine itself. For whatever reason, this did the trick.
Thanks for your thoughts / suggestions.

How to initialize recaptcha in rails app?

I used the following guide: http://www.tweetegy.com/2012/10/setting-up-a-captcha-with-recaptcha-service-and-the-captcha-gem/
I have the following in development.rb:
ENV['RECAPTCHA_PUBLIC_KEY'] = 'keyString'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'keyString'
In config/initializers/recaptcha.rb:
Recaptcha.configure do |config|
config.public_key = RECAPTCHA_PUBLIC_KEY
config.private_key = RECAPTCHA_PRIVATE_KEY
end
I get the following error when I run rails server in development:
Exiting
/home/action/visualhaggard.org/config/initializers/recaptcha.rb:2:in `block in <top (required)>': uninitialized constant RECAPTCHA_PUBLIC_KEY (Nam
eError)
Has anyone encountered and solved this problem? Do I have a typo? Thanks.
The example doesn't define the RECAPTCHA_PUBLIC_KEY in an environment variable. It just defines it as a constant.
#put this in development.rb and in production.rb (separate keys in each so you can test!)
RECAPTCHA_PUBLIC_KEY= 'your-public-key'
RECAPTCHA_PRIVATE_KEY= 'your-private-key'

Ruby - undefined method `extract_options!' : Array

While running following sample using TweetStream I am getting mentioned error.
tweets.rb
require 'tweetstream'
TweetStream.configure do |config|
config.consumer_key = '<CONSUMER KEY>'
config.consumer_secret = '<CONSUMER SECRET>'
config.oauth_token = '<OAUTH TOKEN>'
config.oauth_token_secret = '<OAUTH TOKEN SECRET'
config.auth_method = :oauth
end
TweetStream::Client.new.track('ruby') do |status|
puts "#{status.text}"
end
Error
$ ruby tweets.rb
/home/amit/.rvm/gems/ruby-1.9.3-p194/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:96:in `track': undefined method `extract_options!' for ["ruby"]:Array (NoMethodError)
from tweets.rb:11:in `<main>'
https://github.com/intridea/tweetstream
Am I missing something?
Here's another solution: opening up Array class and defining extract_options! method on it.
Add the following code :
class Array
def extract_options!
last.is_a?(::Hash) ? pop : {}
end unless defined? Array.new.extract_options!
end
to the beginning of the tweets.rb file or to a separate file (which would
need to be required in the tweets.rb file).
extract_options! is ActiveSupport method. If it's not rails app you need to install it and include into script.
I am too late to answer but i think it'll be useful for ruby naive programmers like me.
To include ActiveSupport method like extract_options!, you need to include Active Support.
require 'active_support'
And if you want to include ruby gems then include rubygems too.
require 'rubygems'

cannot split': bad URI(is not URI?):

I've got the following CarrierWave initializer which works fine on my Heroku/MONGOHQ/GridFS env :
CarrierWave.configure do |config|
config.storage = :grid_fs
uri = URI.parse(ENV['MONGOHQ_URL'])
config.grid_fs_database = File.basename(uri.path)
config.grid_fs_host = uri.host unless uri.host.blank?
config.grid_fs_port = uri.port unless uri.port.blank?
config.grid_fs_username = uri.user unless uri.user.blank?
config.grid_fs_password = uri.password unless uri.password.blank?
config.grid_fs_access_url = '/gridfs'
config.cache_dir = "uploads"
config.root = Rails.root.join('tmp')
end
but, when I try to run the code locally (in developement) I get the following error:
`split': bad URI(is not URI?): (URI::InvalidURIError)
here is the full stack : http://pastie.org/1630069 I tried to add require 'uri/generic' on top of initializer but doesn't works.
Does anybody know way ?
Thanks in advance
luca
another solution would be to add a ".env" file in the project root and define there environment variables like:
MONGOHQ_URL=mongodb://someuser:somepass#paulo.mongohq.com:10040/appid
As suggested by KenB the ENV['MONGOHQ_URL'] was not setted on my local machine developement environment :
lsoave#ubuntu:~/rails/heroku/mp3upload$ rails c
Loading development environment (Rails 3.0.5)
ruby-1.9.2-p136 :001 > ENV['MONGOHQ_URL']
=> nil
ruby-1.9.2-p136 :002 >
this was a branch without the initializer, so on my local machine I had to skip that. I did it like that:
if ENV['MONGOHQ_URL']
CarrierWave.configure do |config|
config.storage = :grid_fs
uri = URI.parse(ENV['MONGOHQ_URL'])
config.grid_fs_database = File.basename(uri.path)
config.grid_fs_host = uri.host unless uri.host.blank?
config.grid_fs_port = uri.port unless uri.port.blank?
config.grid_fs_username = uri.user unless uri.user.blank?
config.grid_fs_password = uri.password unless uri.password.blank?
config.grid_fs_access_url = '/gridfs'
config.cache_dir = "uploads"
config.root = Rails.root.join('tmp')
end
end
I think that should be a very better way to skip a Ralis 3.0.5 initializer during the boot process, conditionally to the ENV['MONGOHQ_URL'] parameter value.
If you have a better way, could you please share it ?
Many thanks :-)
luca
In your initializer, you can do this: URI.parse(ENV['MONGOHQ_URL'] || 'some_other_link') as specified in the MongoHQ Heroku docs

Resources