undefined method `each' for #<String:0x00000003a27a58> - ruby-on-rails

First, I do really apologize, since I'm still newbie on this.
I was trying to install Fat Free CRM by following the instruction on this following sites:
http://www.blogdugeek.fr/crm-installation-fat-free-crm-debian-squeeze/
http://guides.fatfreecrm.com/Setup-Linux-or-Mac-OS.html
As I follow the instructions, I've encounter some errors and resolved some. However, upon executing this command:
RAILS_ENV=production rake db:create db:migrate crm:settings:load
I was stuck in this command line and here are the following errors that I've been stuck with:
rake aborted!
NoMethodError: undefined method `each' for #<String:0x00000003a27a58>
/usr/local/rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:150:in `resolve_all'
/usr/local/rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:69:in `resolve'
/usr/local/rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/core.rb:46:in `configurations='
/usr/local/rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:5:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)
As I've search for more related issue, I found some, but it's still no use.
Also, here are some data that might be needed:
Ruby Version
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
Rails Version
Rails 4.2.6
Here are the error lines
connection_specification.rb
def resolve(config)
if config
resolve_connection config
elsif env = ActiveRecord::ConnectionHandling::RAILS_ENV.call
resolve_symbol_connection env.to_sym
else
raise AdapterNotSpecified
end
end
# Expands each key in #configurations hash into fully resolved hash
def resolve_all
config = configurations.dup
config.each do |key, value| <---- Error line
config[key] = resolve(value) if value
end
config
end
connection_handling.rb
class MergeAndResolveDefaultUrlConfig # :nodoc:
def initialize(raw_configurations)
#raw_config = raw_configurations.dup
#env = DEFAULT_ENV.call.to_s
end
# Returns fully resolved connection hashes.
# Merges connection information from `ENV['DATABASE_URL']` if available.
def resolve
Error line ----> ConnectionAdapters::ConnectionSpecification::Resolver.new(config).resolve_all
end
private
def config
#raw_config.dup.tap do |cfg|
if url = ENV['DATABASE_URL']
cfg[#env] ||= {}
cfg[#env]["url"] ||= url
end
end
end
core.rb
def self.configurations=(config)
Error line ---> ##configurations = ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig.new(config).resolve
end
self.configurations = {}
# Returns fully resolved configurations hash
def self.configurations
##configurations
end
databases.rake
db_namespace = namespace :db do task :load_config do
Error line ----> ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
Here's the config/database.yml file.
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
#------------------------------------------------------------------------------
development:&development
adapter:mysql2
encoding:utf8
database:fat_free_crm_development
pool:5
username:root
# password:
socket:/var/run/mysqld/mysqld.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *development
database: fat_free_crm_test
production:
adapter: mysql
encoding: utf8
database: fat_free_crm_production
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
socket: /tmp/mysql.sock
staging:
<<: *development
database: fat_free_crm_staging
Hope to hear and seek some advises and learning.
If there's need some more information, let me know.
Thanks,

Your database.yml is the problem. YAML requires a separator between the key and data.
So not like this:
production:
adapter:mysql
encoding:utf8
...
But like this:
production:
adapter: mysql
encoding: utf8
...
You need to correct all the lines in the file, because you have this error everywhere.

Check the database.yml file again. Don't add anything
ie the file format must be correct.
had unkowngly comment a line on to that was causing the error.

Related

Rails Rspec test fails: Failure/Error: FATAL: database "jmschp" does not exist

I have a project with Rails 5.2.4.5. Where I am using Rspec for testing, and Postgres as database.
RSpec 3.10
- rspec-core 3.10.1
- rspec-expectations 3.10.1
- rspec-mocks 3.10.2
- rspec-support 3.10.2
When I run rails spec:helpers I am getting the following error:
An error occurred while loading ./spec/helpers/pluralize_without_count_helper_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
ActiveRecord::NoDatabaseError:
FATAL: database "jmschp" does not exist
# ./spec/rails_helper.rb:30:in `<top (required)>'
# ./spec/helpers/pluralize_without_count_helper_spec.rb:1:in `<top (required)>'
# ------------------
# --- Caused by: ---
# PG::ConnectionBad:
# FATAL: database "jmschp" does not exist
# ./spec/rails_helper.rb:30:in `<top (required)>'
No examples found.
I have droped the data base and recreated it again.
In my database.yml I have:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 6 } %>
development:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
database: store_development
test:
<<: *default
url: <%= ENV['TEST_DATABASE_URL'] %>
In development work fine.
How can I fix this?
I don't see the name of the test database setup in database.yml you can try below
test:
<<: *default
database: <%= ENV['TEST_DATABASE_URL'] || 'store_development_test' %>
and run this to make sure test database has all the migrations applied.
bin/rails db:test:prepare

carrierwave-dropbox authorization

I have created an app on dropbox and I am following the https://github.com/robin850/carrierwave-dropbox steps but cannot get the access token.
It has 400 bad request although I am providing correct key and app secret but cannot get the link to allow my app.
Here is the error and I am creating a ruby-on-rails app:
rake aborted!
DropboxAuthError: Error getting request token. Is your app key and secret correctly set? Server returned 400: Bad Request.
/var/lib/gems/2.3.0/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:277:in `get_token'
/var/lib/gems/2.3.0/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:293:in `get_request_token'
/var/lib/gems/2.3.0/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:299:in `get_authorize_url'
/var/lib/gems/2.3.0/gems/carrierwave-dropbox-1.2.1/lib/carrierwave/dropbox/rake.rb:13:in `authorize'
/var/lib/gems/2.3.0/gems/carrierwave-dropbox-1.2.1/lib/carrierwave/dropbox/authorize.rake:11:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => dropbox:authorize
(See full trace by running task with --trace)
Have you configured carrierwave
CarrierWave.configure do |config|
config.dropbox_app_key = ENV["APP_KEY"]
config.dropbox_app_secret = ENV["APP_SECRET"]
config.dropbox_access_token = ENV["ACCESS_TOKEN"]
config.dropbox_access_token_secret = ENV["ACCESS_TOKEN_SECRET"]
config.dropbox_user_id = ENV["USER_ID"]
config.dropbox_access_type = "dropbox"
end
Here is an example hope it can work:
1- create a file config/config.yml and provide here your keys:
default: &default
development:
<<: *default
app_key: "your_app_key"
production: &production
<<: *default
app_key: "your_app_key"
staging: &staging
<<: *default
app_key: "your_app_key"
2 - in environment.rb file
# Load the Rails application.
require_relative 'application'
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
# Initialize the Rails application.
Rails.application.initialize!
3 - then you can use in in carrierwave.rb file
CarrierWave.configure do |config|
config.dropbox_app_key = APP_CONFIG['app_key']
//and your another configuration so on...
}
end
if you have right api keys permission then it should work. thanks

KeyError: Cannot load `Rails.application.database_configuration`: key not found: "DB_ENV_POSTGRESQL_USER"

So I have been looking everywhere to try and find a solution to this and i have had no luck up to now. I'm ruby on rails so i could very well be missing something basic or be looking in the wrong place for the solution, so i thought i'd just ask. I have a rails API which i forked from my organization and I want to run it locally. I got stuck running this line in the terminal
bundle exec rake create:db
and got this error, I don't really know what to do :/ If this question has already been answered just point me there and i'll go figure it out! Thank you.
Admins-MacBook-Pro:environments nathanshanko$ bundle exec rake db:create
(in /Users/nathanshanko/Desktop/git-repos/voke_api)
rake aborted!
KeyError: Cannot load `Rails.application.database_configuration`:
key not found: "DB_ENV_POSTGRESQL_USER"
(erb):7:in `fetch'
(erb):7:in `'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/application/configuration.rb:104:in `database_configuration'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:41:in `block (3 levels) in '
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `'
KeyError: key not found: "DB_ENV_POSTGRESQL_USER"
(erb):7:in `fetch'
(erb):7:in `'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/application/configuration.rb:104:in `database_configuration'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:41:in `block (3 levels) in '
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `'
Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)
Here is my database.yml file
common: &common
adapter: postgresql
encoding: utf8
reconnect: true
pool: <%= ENV['DB_ENV_POSTGRESQL_POOL'] || 5 %>
database: <%= ENV['DB_ENV_POSTGRESQL_DB'] || 'voke_api' %>
username: <%= ENV.fetch('DB_ENV_POSTGRESQL_USER') %>
password: <%= ENV['DB_ENV_POSTGRESQL_PASS'] %>
host: <%= ENV.fetch('DB_PORT_5432_TCP_ADDR') %>
port: <%= ENV['DB_PORT_5432_TCP_PORT'] %>
development:
<<: *common
production:
<<: *common
staging:
<<: *common
test:
<<: *common
It sounds like you're missing some env vars that define your database credentials. You'll want to set these up (either via the command line, or something like a .env or application.yml file, depending on your project set up).
The error you're getting on DB_ENV_POSTGRESQL_USER is due to the ENV.fetch, which raises an IndexError if the key isn't found. You may also be missing other env vars, but ENV['xyz'] will fail silently without throwing any errors.

Rails: constant not initializing

I am trying to use different stripe keys (a credit card payment processing system) depending on whether I'm in test/development or production/ Based on suggestions I've seen on StackOverflow, I did the following:
In my /config/initalizers/stripe.rb file, I have the following:
STRIPE_CONFIG = begin
config = YAML.load(File.open(Rails.root.join('config', 'stripe.yml')))
config = config[Rails.env] || {}
config.to_options
end
and in my /config/stripe.yml file, I have the following:
default: &default
Stripe.api_key: "testapikeycode"
STRIPE_PUBLIC_KEY: "testpublickeycode"
development:
<<: *default
test:
<<: *default
production:
Stripe.api_key: "productionapikeycode"
STRIPE_PUBLIC_KEY: "productionpublickeycode"
However, when I go into the console (rails console), and I type
puts STRIPE_PUBLIC_KEY
I get the following error message:
NameError: uninitialized constant STRIPE_PUBLIC_KEY
Any ideas?
Alternate method
Only use stripe.rb, and have the following in it:
if Rails.env == 'production'
Stripe.api_key: "productionapikeycode"
STRIPE_PUBLIC_KEY: "productionpublickeycode"
else
Stripe.api_key: "tesapikeycode"
STRIPE_PUBLIC_KEY: "testpublickeycode"
end
STRIPE_PUBLIC_KEY is a key in your yaml config file, you never actually initialize it as a constant. If you type p STRIPE_CONFIG['STRIPE_PUBLIC_KEY'] instead you should get the result you want.
Aside I think YAML.load should be YAML.load_file.

Rails with Mongo Mapper

This is turning out to be the worst day for learning rails. Already failed to connect and configure to sqlite3, mysql databases I turned to mongo mapper and facing the same issues. When I try to start the rails server I get the following:
/usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require': no such file to load -- mongo_mapper (LoadError)
I have already installed mongo_mapper gem but when I do 'which mongo_mapper' it does not return anything.
Here is my mongo_config.rb file placed inside the initializers folder:
require "rubygems"
require "mongo_mapper"
MongoMapper.database = "blog-#{Rails.env}"
UPDATE 1:
I changed to the following:
require "rubygems"
include MongoMapper
MongoMapper.database = "blog-#{Rails.env}"
And now I get the following error:
/Users/azamsharp/Projects/railsprojects/blog/config/initializers/mongo_config.rb:2: uninitialized constant MongoMapper (NameError)
You must use bundler with rails 3. don't try to not use it. add the following to your Gemfile
source 'http://gemcutter.org'
source 'http://rubygems.org'
gem "rails", "3.0.7"
gem 'bson', "1.1.1"
gem 'bson_ext', "1.1.1"
gem 'mongo_mapper'
then run
bundle install
then rails server will work
In order to get mongo_mapper working in Rails (tested under Rails 3.1, should work in Rails 3.0.x as well), you have to follow these steps:
In Gemfile, add the mongo_mapper and bson gem:
gem 'mongo_mapper'
gem 'bson_ext'
Run bundle install
In the config/ directory, create a mongo.yml file like this one:
# config/mongo.yml
defaults: &defaults
host: 127.0.0.1
port: 27017
development:
<<: *defaults
database: db_development
username: user
password: password
test:
<<: *defaults
database: db_test
# set these environment variables on your prod server
production:
<<: *defaults
database:
username:
password:
In the config/initializers directory, create a mongo.rb file like this:
# config/initializers/mongo.rb
#include MongoMapper
db_config = YAML::load(File.read(File.join(Rails.root, "/config/mongo.yml")))
#
if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
mongo = db_config[Rails.env]
MongoMapper.connection = Mongo::Connection.new(mongo['host'] || 'localhost',
mongo['port'] || 27017,
:logger => Rails.logger)
MongoMapper.database = mongo['database']
if mongo['username'] && mongo['password']
MongoMapper.database.authenticate(mongo['username'], mongo['password'])
end
end
# Used for image uploads
# CarrierWave.configure do |config|
# mongo = db_config[Rails.env]
# config.grid_fs_database = mongo['database']
# config.grid_fs_host = mongo['host'] || 'localhost'
# config.grid_fs_access_url = "gridfs"
# config.grid_fs_username = mongo['username']
# config.grid_fs_password = mongo['password']
# end
# It's also possible to define indexes in the the model itself; however,
# a few issues are being worked out still. This is a temporary solution.
# Comment.ensure_index([["story_id", 1], ["path", 1], ["points", -1]])
# MongoMapper.ensure_indexes!
# Handle passenger forking.
# if defined?(PhusionPassenger)
# PhusionPassenger.on_event(:starting_worker_process) do |forked|
# MongoMapper.database.connect_to_master if forked
# end
# end
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect if forked
end
end
Start the mongod server before starting the rails server and enjoy!
Try removing the require lines and replace them with include MongoMapper

Resources