ruby-on-rails - 在 Rails 应用程序中请求/响应 Soap API

标签 ruby-on-rails ruby ruby-on-rails-4 soap

我需要从 Rails 4 应用程序中的 SOAP API 网络服务 (XML) 请求一些数据。用户输入他的电子邮件和确认号码,并收到一堆关于他的预订的信息。

这是在我创建的一个单独的应用程序中工作的,没有指定正确的路线并使用 <%= form_tag(root_url, :method => :get ) %>在索引 View 中。完成的应用程序需要一个适当的路由结构,但是当我输入 <%= form_tag('searches/show', :method => :get ) %> 时,我无法从 Soap API 获得任何响应。在我的新应用程序的 View 中。 (我只是得到一个空白屏幕,而旧版本工作正常)

目前我正在使用硬编码字符串从索引路由到显示页面。我知道这可能不是最佳实践,但由于这是从 Soap API 调用信息,因此 Rails 应用程序背后不需要数据库。 (尽管我已经放了一个)

无论如何,提前感谢您的帮助,这是我认为目前相关的应用程序的所有部分。如果您需要更多信息,请告诉我!

索引 View (/views/searches/index.html.erb):

<h1>Manage your Booking</h1>

<p>
  <%= form_tag('searches/show', :method => :get ) %>
  <%= label_tag 'Confirmation Number:' %>
  <%= text_field_tag :confirmation_number %>

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

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

显示 View (/views/searches/show.html.erb)

<% if @search %>
  <dl id ="reservation_info">
    <dt>Test Text:</dt>
    <dd><%= @search.reservation_id %></dd>
    <dd><%= @search.dining_date_and_time %></dd>
    <dd><%= @search.size %></dd>
    <dd><%= @search.session_id %></dd>
    <dd><%= @search.first_name %></dd>
    <dd><%= @search.last_name %></dd>    
    <dd><%= @search.confirm_number %></dd>
    <dd><%= @search.allowed_to_cancel_online %></dd>
    <dd><%= @search.restaurant_phone_number %></dd>
    <dd><%= @search.restaurant_id %></dd>
    <dd><%= @search.restaurant_name %></dd>
    <dd><%= @search.location_id %></dd>
    <dd><%= @search.location_name %></dd>
  </dl>
<% end %>

Controller (controllers/searches_controller.rb)

class SearchesController < ApplicationController
    def index   
      if params[:confirmation_number] && params[:email]   
        @search = Search.new params[:confirmation_number], params[:email]
      end
    end

  private 

    def search_params
      params.require(:search).permit(:confirmation_number, :email)
    end

end

Routes config file:

Rails.application.routes.draw do

  resources :reservations do
  end

  resources :searches do
  end

end

模型(models/search.rb):

class Search < ActiveRecord::Base

attr_accessor :reservation_id, :dining_date_and_time, :size, :session_id, :first_name, :last_name, :confirm_number, :allowed_to_cancel_online, :restaurant_phone_number, :restaurant_id, :restaurant_name, :location_id, :location_name

def client
  client = Savon.client(wsdl: "http://wsdl-example-url-placeholder?wsdl", follow_redirects: :follow_redirects)
end

def initialize(confirmation_number, email)
  message = {'ConfirmationNumber' => confirm_number, 'EMail' => email  }
  response = client.call(:search_for_reservation, message: message)
  if response.success?
    data = response.to_array(:search_for_reservation_response, :reservation).first
    if data
      @reservation_id = data[:@id]
      @dining_date_and_time = data[:dining_date_and_time]
      @size = data[:size]
      @session_id = data[:session_id]
      @first_name = data[:first_name]
      @last_name = data[:last_name]
      @confirm_number = data[:confirmation_number]
      @allowed_to_cancel_online = data[:allowed_to_cancel_online]
      @restaurant_phone_number = data[:restaurant_phone_number]

      data2 = response.to_hash[:search_for_reservation_response][:reservation][:restaurant]
      if data2
        @restaurant_id = data2[:@id]
        @restaurant_name = data2[:name]

        data3 = response.to_hash[:search_for_reservation_response][:reservation][:restaurant][:location]
        if data3
         @location_id = data3[:@id]
         @location_name = data3[:name]
        end
      end
    end
  end
end

终端输出:

Started GET "/searches" for ::1 at 2015-07-08 14:39:56 +0100
  ActiveRecord::SchemaMigration Load (1.4ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by SearchesController#index as HTML
  Rendered searches/index.html.erb within layouts/application (2.9ms)
Completed 200 OK in 296ms (Views: 285.8ms | ActiveRecord: 0.0ms)


Started GET "/searches" for ::1 at 2015-07-08 14:39:56 +0100
Processing by SearchesController#index as HTML
  Rendered searches/index.html.erb within layouts/application (0.3ms)
Completed 200 OK in 48ms (Views: 47.7ms | ActiveRecord: 0.0ms)


Started GET "/searches/show?utf8=%E2%9C%93&confirmation_number=ZANHJW5U&email=myemail%40email.com" for ::1 at 2015-07-08 14:40:06 +0100
Processing by SearchesController#show as HTML
  Parameters: {"utf8"=>"✓", "confirmation_number"=>"ZANHJW5U", "email"=>"myemail@email.com", "id"=>"show"}
  Rendered searches/show.html.erb within layouts/application (0.5ms)
Completed 200 OK in 49ms (Views: 48.2ms | ActiveRecord: 0.0ms

最佳答案

我认为您正在向 searches/show 页面发出请求,但您没有针对该页面的操作?您有一个索引操作,因此请更改表单以发出仅 searches 的请求。这能解决问题吗?

关于ruby-on-rails - 在 Rails 应用程序中请求/响应 Soap API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31294949/

相关文章:

ruby-on-rails - 使用 ActiveRecord 方法构造对象? (Rails 上的 ruby )

ruby-on-rails - ActiveRecord 有两个关联

javascript - 在加载 react js 之前存储 JS 变量

ruby - Time.now - 1.hour.ago => 3599.999604?

ruby-on-rails - Redis To Go 等附加组件对 Heroku/EC2 安全吗?

ruby-on-rails - 使用描述字段的大小作为权重将@blogs 拆分为三个 div

ruby-on-rails - 从 Rails 的任何地方访问 "session key"cookie 名称

ruby-on-rails - 有人可以简单地向我解释什么是 Passenger 吗?

数组中的 Ruby block

mysql - 导轨 4 : can't save a record to db