Uninitialized constant ActionDispatch::Routing::Routes (NameError)

CodeGeek123 picture CodeGeek123 · Oct 11, 2011 · Viewed 9.4k times · Source

I'm following a clickatell tutorial and my code looks as follows. However I get the error

uninitialized constant ActionDispatch::Routing::Routes (NameError)
from /Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:258:in `instance_exec'
    from /Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:258:in `eval_block'
    from /Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:235:in `draw'
    from /Users/user1/Desktop/rails_projects/clickatellsms/config/routes.rb:1
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:29:in `load_paths'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:29:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:29:in `load_paths'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:13:in `reload!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:7:in `initialize'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.1/lib/active_support/file_update_checker.rb:32:in `call'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.1/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/finisher.rb:63
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/finisher.rb:64:in `call'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/finisher.rb:64
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:30:in `instance_exec'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:30:in `run'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:55:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:54:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:54:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application.rb:96:in `initialize!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/railtie/configurable.rb:30:in `send'
    from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/user1/Desktop/rails_projects/clickatellsms/config/environment.rb:5
    from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:4:in `require'
    from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:4
    from /Library/Ruby/Gems/1.8/gems/rack-1.3.4/lib/rack/builder.rb:51:in `instance_eval'
    from /Library/Ruby/Gems/1.8/gems/rack-1.3.4/lib/rack/builder.rb:51:in `initialize'
    from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:1:in `new'
    from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:1

My code looks like this -

# controllers/sms.rb:
require 'clickatell'

class SMS

  def initialize(config)
   @config = config
  end

  def create(recipient, message_text)
   api.send_message(recipient, message_text)
  end

  private

   def api
    @api ||= Clickatell::API.authenticate(
    @config[:api_key],
    @config[:username],
    @config[:password])
  end

end

 # config/clickatell.yml:
 api_key: 9999999
 username: abcdefg
 password: hijklmno

# config/environments/development.rb
CLICKATELL_CONFIG = YAML.load(File.open(File.join(RAILS_ROOT,
'config', 'clickatell.yml')))

# config/routes.rb:
    ActionController::Routing::Routes.draw do |map|
   map.resource :sms
end

# app/views/sms/new.html.erb:
 <% form_tag '/sms', :method => :post do -%>
 <label>Enter the recipients mobile number:</label>
 <%= text_field_tag "recipient" %>
 <label>Enter your message:</label>
 <%= text_area_tag "message_text" %>
 <%= submit_tag "Send SMS" %>
 <% end %>

# controllers/sms_controller.rb:
class SmsController < ApplicationController
def create
  sms = SMS.new(CLICKATELL_CONFIG)
  sms.create(params[:recipient], params[:message_text])
  flash[:notice] = "Message sent succesfully!"
  redirect_to :back
  rescue Clickatell::API::Error => e
  flash[:error] = "Clickatell API error: #{e.message}"
  redirect_to :back
 end
 end

Answer

apneadiving picture apneadiving · Oct 11, 2011

That's your routes.rb which is non sense:

ActionController::Routing::Routes.draw do |map|
  map.resource :sms
end

It should rather be:

APP_NAME_HERE_CAMEL_CASE_STYLE::Application.routes.draw do
  resource :sms
end

Looks like you copy/pasted old Rails 2 code within a Rails 3 app.