NameError at / uninitialized constant SearchReservationController::Search

1.6k views Asked by At

I am trying to make requests to a SOAP XML webservice/api by creating a simple index view and controller. The user inputs 2 simple pieces of information: a confirmation number and email address and he receives back a response from the webservice giving him information about his booking.

This all works in a single ruby file, outputting the correct information to the console. But I am having a lot of difficulty integrating it into a Rails 4 project. I am stuck with this error:

better errors uninitialized constant message

I put my original ruby file search_reservation.rb in Rails' lib folder, wrapped the class in a module (Search) and then changed application.rb to include this line: config.autoload_paths << Rails.root.join('lib')

Here's my full error trace from the terminal:

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"=>"[email protected]"}
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

There are 2 answers

5
jon snow On

Try by changeing in SearchReservationController#index on line

 search_reservation = Search::SearchReservation.new

with

search_reservation = SearchReservation.new

Or, may try wrap SearchReservation class in Search module.

class Search::SearchReservation

And yes... change this in /application.rb and restart the app,

config.autoload_paths << Rails.root.join('lib').to_s
2
Piotr Kruczek On

The problem here is that rails tries to access the Search module in the controller's namespace.

Try forcing "main" namespace using :: like this

search_reservation = ::Search::SearchReservation.new

I'm also not sure about your autoload, try changing its format to

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