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

发布于 2024-12-09 01:32:47 字数 4390 浏览 0 评论 0原文

我正在遵循 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2024-12-16 01:32:47

这是你的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 代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文