Загрузка пользовательских данных в гем Hexorx/ страны в рельсах 4.2
Я использую драгоценные страны для заполнения элемента выбора страны. Я хотел бы добавить пользовательские страны в список стран ISO3116, представленных жемчужиной.
Gem: https://github.com/hexorx/countries
Я прочитал вики для драгоценных стран. Я пытался следовать инструкциям, приведенным в разделе "Загрузка пользовательских данных". к сожалению их слишком мало для родственника рельсов новичка вроде меня.
Когда я пытаюсь загрузить сервер разработки, я получаю сообщение об ошибке, приведенное ниже.
Я не знаю, где я должен добавить следующий код. Я добавил его в "config/initializers", потому что согласно руководствам ( http://guides.rubyonrails.org/initialization.html) файлы в этом каталоге загружаются после загрузки файлов gem при инициализации приложения. Отсюда мое замешательство. К тому времени, когда код, приведенный ниже, будет выполнен, не должна ли константа 'ISO3116' быть инициализирована?
Код:
конфиг / Инициализаторы / custom_countries.rb
ISO3116::Data.register(
continent: "Europe",
alpha2: "EN",
alpha3: "ENG",
name: "England"
)
Gem file
source 'https://rubygems.org'
#default:
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
#jQuery plugin for drop-in fix binded events problem caused by Turbolinks
gem 'jquery-turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
#Optional Defaults:
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
#locales, inc en-GB
gem 'rails-i18n'
#OS:
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
#Servers:
#Thin web server, better than stock server
gem 'thin'
#Debugging:
group :development do #Development. Avoid using in production.
#Display compile/runtime errors better in browser
gem "better_errors"
#Required for better_errors advanced features (REPL, variable incpection)
gem "binding_of_caller"
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
end
group :development, :test do #Development & Test Avoid using in production.
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
#Models:
#Pagination
gem 'kaminari'
#search
gem 'filterrific'
#ISO 3166 countries list
gem 'countries'
gem 'country_select'
#Views:
#animation of page loads
gem "animate-rails"
#Simple Form, for creating froms
gem 'simple_form'
#nested forms
gem 'cocoon'
#Bootstrap, front-end web development framework to inetgrate with rails
gem 'bootstrap-sass', '~> 3.2.0'
#Allow you to write CSS (SASS, LESS etc) rules without vendor prefixes.
gem 'autoprefixer-rails'
#Override the browser default confirmation pop up with a bootsrap modal
gem 'data-confirm-modal'
#Testing:
group :development, :test do
#Exit test-suite execution at first test-failure.
gem 'minitest-fail-fast'
#Takes development database and converts to fixtures for testing
gem 'fixtures_dumper'
#Used to generate fake data
gem 'faker'
end
Ошибка сервера при попытке загрузки
C:\Users\Patrick\Desktop\Consign>rails server -b 0.0.0.0 -p 80
=> Booting Thin
=> Rails 4.2.5.1 application starting in development on http://0.0.0.0:80
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
C:/Users/Patrick/Desktop/Consign/config/initializers/custom_countries.rb:1:in `<
top (required)>': uninitialized constant ISO3116 (NameError)
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/engine.rb:652:in `block in load_config_initializer'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-
4.2.5.1/lib/active_support/notifications.rb:166:in `instrument'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/engine.rb:651:in `load_config_initializer'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/engine.rb:615:in `each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/engine.rb:615:in `block in <class:Engine>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/initializable.rb:30:in `instance_exec'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/initializable.rb:30:in `run'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block i
n tsort_each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (
2 levels) in each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:418:in `block (
2 levels) in each_strongly_connected_component_from'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_st
rongly_connected_component_from'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:417:in `block i
n each_strongly_connected_component_from'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/initializable.rb:44:in `each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/initializable.rb:44:in `tsort_each_child'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:411:in `call'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:411:in `each_st
rongly_connected_component_from'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block i
n each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_st
rongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_e
ach'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_e
ach'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/initializable.rb:54:in `run_initializers'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/application.rb:352:in `initialize!'
from C:/Users/Patrick/Desktop/Consign/config/environment.rb:6:in `<top (
required)>'
from C:/Users/Patrick/Desktop/Consign/config.ru:3:in `require'
from C:/Users/Patrick/Desktop/Consign/config.ru:3:in `block in <main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/builder.rb:55:in `instance_eval'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/builder.rb:55:in `initialize'
from C:/Users/Patrick/Desktop/Consign/config.ru:in `new'
from C:/Users/Patrick/Desktop/Consign/config.ru:in `<main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/builder.rb:49:in `eval'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/builder.rb:49:in `new_from_string'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/builder.rb:40:in `parse_file'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/server.rb:299:in `build_app_and_options_from_config'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/server.rb:208:in `app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands/server.rb:61:in `app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.5/lib
/rack/server.rb:336:in `wrapped_app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands/server.rb:139:in `log_to_stdout'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands/server.rb:78:in `start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands/commands_tasks.rb:75:in `tap'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands/commands_tasks.rb:75:in `server'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.5
.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Спасибо, PQ
PS. Это мой второй (возможно, третий) вопрос, который я задал на Stackru. Обратная связь приветствуется.
1 ответ
FML.
Мой подход правильный (ну, это работает). Код можно поместить в config/initializer, моя ошибка произошла из-за неверно отформатированного хэша, переданного в:
ISO3166::Data.register()
Надеюсь это поможет.