title: building generator in gem for rails 4
body: <p>I have looked and tried various things in regards to creating a gem with a generator.  Maybe my tiredness is causing me to forget something or maybe its just my lack of experience.  Either way I am trying to understand how to build a gem that is a simple generator so that I can reuse the code in future projects.  Yes I am building something that already exists but as a learner I am more interested in understanding how to build a gem so that I may be able to contribute something more meaningful in the future rather than just using already made gems without knowing what is really going on.  So with out further ado my code looks like this:</p>
  
  <pre><code>tree for simpauth
  -lib
   -generators
    -simpauth
     -templates
      sessions.rb
     install_generator.rb
   -simpauth
   simpauth.rb
  </code></pre>
  
  <p>here is my code for generators/simpauth/install_generator.rb</p>
  
  <pre><code>require 'rails/generators'
  
  module Simpauth
    class InstallGenerator &lt; ::Rails::Generators::Base
      source_root File.expand_path('../templates', __FILE__)
      desc "Creating simple and customizable authentication"
  
      def add_session
          copy_file "sessions.rb", "app/controllers/sessions_controller.rb"
      end
    end
  end
  </code></pre>
  
  <p>my generators/simpauth/templates/sessions.rb</p>
  
  <pre><code>class SessionsController &lt; ApplicationController
    def new
    end
  
    def create
      user = User.find_by(email: params[:session][:email])
      if user &amp;&amp; user.authenticate(params[:session][:password])
          #login user and redirect to user_path
          log_in user
          params[:session][:remember_me] == '1' ? remember(user) : forget(user)
          redirect_to user
      else
          flash.now[:danger] = "invalid email and/or password"
          render 'new'
      end
    end
  
    def destroy
      log_out if logged_in?
      redirect_to root_url
    end
  
  end
  </code></pre>
  
  <p>and lib/simpauth.rb </p>
  
  <pre><code>require "simpauth/version"
  require 'rails'
  
  module Simpauth
    class Engine &lt; Rails::Engine
    end
  end
  </code></pre>
  
  <p>also simpauth.gemspec</p>
  
  <pre><code># coding: utf-8
  $:.push File.expand_path('../lib', __FILE__)
  require 'simpauth/version'
  
  Gem::Specification.new do |spec|
    spec.name          = "simpauth"
    spec.version       = Simpauth::VERSION
    spec.authors       = ["My Name"]
    spec.email         = ["my_email@example.com"]
    spec.summary       = %q{Simplified authentication}
    spec.description   = %q{}
    spec.homepage      = ""
    spec.license       = "MIT"
  
    spec.files         = `git ls-files -z`.split("\x0")
    spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
    spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
    spec.require_paths = ["lib"]
  
    spec.add_development_dependency "bundler", "~&gt; 1.7"
    spec.add_development_dependency "rake", "~&gt; 10.0"
  end
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Edit - this code works as expected inside a rails app.  I just can't  get rails to recognize the generator when installed as a gem.</p>