Rails how to create an initializer inside a gem

前端 未结 1 1152
北荒
北荒 2021-01-02 11:16

I am trying to build a gem in Rails 3 and inside it i am trying to pass an initializer:

Credentials.configure do |config|
  file = File.read(\"#{Rails.root}/         


        
1条回答
  •  走了就别回头了
    2021-01-02 11:39

    Your gem will first need to define a generator in lib/generators/your_gem_name_generator.rb

    mkdir -p lib/generators/
    

    Copy your initializer in that folder with a name like twitter_credentials.rb

    Then create another file in that folder with a name like twitter_generator.rb with content like this:

    class YourGemNameRailtie < Rails::Generators::Base
      source_root(File.expand_path(File.dirname(__FILE__)))
      def copy_initializer
        copy_file 'twitter_credentials.rb', 'config/initializers/twitter_credentials.rb'
      end
    end
    

    You should check out the official documentation for creating a generator here: http://guides.rubyonrails.org/generators.html

    0 讨论(0)
提交回复
热议问题