gpt4 book ai didi

ruby-on-rails - 未初始化的常量 ActionDispatch::Routing::Routes (NameError)

转载 作者:数据小太阳 更新时间:2023-10-29 07:04:01 25 4
gpt4 key购买 nike

我正在学习 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

最佳答案

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

关于ruby-on-rails - 未初始化的常量 ActionDispatch::Routing::Routes (NameError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7729543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com