NameError / atinitialized常量SearchReservationController :: Search

时间:2015-06-10 11:15:45

标签: ruby-on-rails xml ruby-on-rails-4 soap-client savon

我试图通过创建一个简单的索引视图和控制器来向SOAP XML webservice / api发出请求。用户输入2条简单的信息:确认号码和电子邮件地址,他收到来自网络服务的回复,向他提供有关其预订的信息。

这一切都在单个ruby文件中工作,将正确的信息输出到控制台。但是我将它集成到Rails 4项目中有很多困难。我坚持这个错误:

better errors uninitialized constant message

我将原始ruby文件search_reservation.rb放在Rails的lib文件夹中,将类包装在模块中(搜索),然后更改application.rb以包含此行:config.autoload_paths << Rails.root.join('lib')

这是终端的完整错误跟踪:

Started GET "/?utf8=%E2%9C%93&confirm_number=NTSOTHJN&email=otherjamesgray%40gmail.com" for ::1 at 2015-06-10 11:57:42 +0100
Processing by SearchReservationController#index as HTML
  Parameters: {"utf8"=>"✓", "confirm_number"=>"NTSOTHJN", "email"=>"otherjamesgray@gmail.com"}
Completed 500 Internal Server Error in 3ms

NameError - uninitialized constant SearchReservationController::Search:
  activesupport (4.2.0) lib/active_support/dependencies.rb:533:in `load_missing_constant'
  activesupport (4.2.0) lib/active_support/dependencies.rb:184:in `const_missing'
  app/controllers/search_reservation_controller.rb:4:in `index'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
  rack (1.6.1) lib/rack/etag.rb:24:in `call'
  rack (1.6.1) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.1) lib/rack/head.rb:13:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.1) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.1) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.1) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.1) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.1) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  rack (1.6.1) lib/rack/content_length.rb:15:in `call'
  rack (1.6.1) lib/rack/handler/webrick.rb:89:in `service'
  /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

search_reservation.rb:

# /lib/search_reservation.rb

module Search

class SearchReservation

def client
client = Savon.client(wsdl: "http://placeholder-for-wsdl-link", adapter: :curb, follow_redirects: :follow_redirects)
end

def first_name(confirm_number, email) 
  message = { 'ConfirmationNumber' => confirm_number, 'EMail' => email }
  response = client.call(:search_for_reservation, message: message)
  response.body[:search_for_reservation_response][:reservation][:first_name]
end

def last_name(confirm_number, email)
  message = { 'ConfirmationNumber' => confirm_number, 'EMail' => email }
  response = client.call(:search_for_reservation, message: message)
  response.body[:search_for_reservation_response][:reservation][:last_name]
end

def size(confirm_number, email)
  message = { 'ConfirmationNumber' => confirm_number, 'EMail' => email }
  response = client.call(:search_for_reservation, message: message)
  response.body[:search_for_reservation_response][:reservation][:size]
end

def session_id(confirm_number, email)
  message = { 'ConfirmationNumber' => confirm_number, 'EMail' => email }
  response = client.call(:search_for_reservation, message: message)
  response.body[:search_for_reservation_response][:reservation][:session_id]
end

end
end 

search_reservation_controller.rb:

# /app/controllers/search_reservations_controller.rb

class SearchReservationController < ApplicationController
  def index    
    if params[:confirm_number] && params[:email]   
      search_reservation = Search::SearchReservation.new
      @first_name = search_reservation.first_name(params[:confirm_number], params[:email])
      @last_name = search_reservation.last_name(params[:confirm_number], params[:email])
      @size = search_reservation.size(params[:confirm_number], params[:email])
      @session_id = search_reservation.session_id(params[:confirm_number], params[:email])
    end
  end  

end

index.html.erb:

# /app/views/search_reservations/index.html.erb

<h1>Search for your reservation</h1>

<p>
  <%= form_tag(root_url, :method => :get ) %>
  <%= label_tag 'Confirmation Number:' %>
  <%= text_field_tag :confirm_number %>

  <%= label_tag 'Email:' %>
  <%= text_field_tag :email %>

  <%= submit_tag "Lookup", name: nil %>
</p>

<% if @search_reservation %>
  <dl id ="reservation_info">
    <dt>Number of people / size of booking:</dt>
    <dd><%= @search_reservation.size %></dd>
    <dt>First Name:</dt>
    <dd><%= @search_reservation.first_name %></dd>
    <dt>Last Name:</dt>
    <dd><%= @search_reservation.last_name %></dd>
    <dt>Meal session:</dt>
    <dd><%= @search_reservation.session_id %></dd>
  </dl>
<% end %>

2 个答案:

答案 0 :(得分:0)

尝试在线上更改SearchReservationController#index

 search_reservation = Search::SearchReservation.new

search_reservation = SearchReservation.new

或者,可以尝试在Search模块中包装SearchReservation类。

class Search::SearchReservation

是的...在/application.rb中更改此内容并重新启动应用

config.autoload_paths << Rails.root.join('lib').to_s

答案 1 :(得分:0)

这里的问题是rails尝试访问控制器命名空间中的Search模块。

尝试强迫&#34;主要&#34;使用::这样的命名空间

search_reservation = ::Search::SearchReservation.new

我也不确定您的自动加载,尝试将其格式更改为

config.autoload_paths += %W(#{Rails.root}/lib)