未初始化的常量ActionDispatch :: Routing :: Routes(NameError)

时间:2011-10-11 16:45:31

标签: ruby-on-rails ruby ruby-on-rails-3 sms clickatell

我正在关注clickatell教程,我的代码如下所示。但是我收到了错误

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

我的代码看起来像这样 -

# 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

1 个答案:

答案 0 :(得分:11)

这是你的routes.rb,这是无意义的:

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

应该是:

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

看起来你在Rails 3应用程序中复制/粘贴了旧的Rails 2代码。