将表单数据传递给xml

时间:2014-01-08 01:30:53

标签: ruby-on-rails

我想从下面的表单中传递我的表单数据

<%= form_for(@order) do |f| %>
  <% if @order.errors.any? %>
    <div id="error_explanation">
      <% @order.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </div>
  <% end %>
  <% if notice %>
    <div id="error_explanation">
      <%= notice %>
    </div>
  <% end %>

    <%= f.hidden_field :branch_id, :value=>@line_item.product.branch_id %>

    <%= f.hidden_field :order_total, :value=>@cart.total_price %>

  <div class="field">
    <%= f.label 'Your Name*' %> :<br />
    <%= f.text_field :name, :size => 40 %>
  </div>

  <div class="field">
    <%= f.label 'Email(optional)' %> :<br />
    <%= f.email_field :email, :size => 40 %>
  </div>

  <div class="field">
    <%= f.label 'Pay_Type*' %> :<br />
    <%= f.select :pay_type, Order::PAYMENT_TYPES, :prompt => 'Select a payment method' %>
  </div>

  <div class="field">
    <%= f.label 'Phone_no*' %> :<br />
    <%= f.text_field :phone_no %><br />
  </div>

  <div class="field">
    <%= f.label 'Time of picking or delivery*' %> :<br />
    <%= f.time_select(:delivery_time, :ampm=>true, :minute_step=>10) %><br />
  </div>

  <div style="margin-left:100px;">
    <%= f.radio_button :delivery, true, :onClick=>"validator()" %> Pick from supermarket<br/>
    <%= f.radio_button :delivery, true, :onClick=>"amount(); validator2()" %> Deliver at premises<br/>
  </div>

  <div class="panel-body">
    <%= f.label :address, "Location (street, plot ,area, division, village)" %> :<br />
    <%= f.text_area :address, :rows => 3, :cols => 40 %>
  </div>  
  <div class="actions">
    <%= f.submit 'Submit Order', :class=>"btn btn-medium btn-info" %>
  </div>
<% end %>

我的订单控制器中的XML表单,如下所示

 post_xml = "<?xml version='1.0' encoding='utf-8'?>
            <AutoCreate>
              <Request>
                <DigitalSignature>de96d901b3bad1db2aab76b7b0b202f2</DigitalSignature>" +

                "<TranAmount>#{@cart.total_price}</TranAmount>" +

                "<TranCharge>#{@cart.service_charge}</TranCharge>" +

                "<ToAccount>#{@line_item.product.seller.link}</ToAccount>" +

                "<FromAccount>#{params[:phone_no]} </FromAccount>" +

              "</Request>
            </AutoCreate>"

这样我就可以将它传递给下面的URI

require 'open-uri'
    require 'rexml/parsers/sax2parser'
    pegPayStatusCode = ""
    con = Net::HTTP.new(url.host, url.port)
       if url.scheme == 'https'
        require 'net/https'
        con.use_ssl = true
              con.verify_mode = OpenSSL::SSL::VERIFY_NONE   # needed for windows environment
      end
    resp, req = con.post(url.path, req, headers)
    puts req

    #checking whether the url is correct to be passed
    #if url=~ URI::regexp
      parser = REXML::Parsers::SAX2Parser.new(@order.phone_no)

但是从服务器运行错误后说“订单不是有效的输入流。它必须走路 比如String,IO或Source。“ 我该怎么办,因为我想在保存订单之前先通过。

1 个答案:

答案 0 :(得分:0)

要传递表单数据,请首先按上述方法定义xml表单。然后我将其传递给Web服务,如下所示

require 'net/http'
    require 'open-uri'

    peg_pay_status_code = ""
    conn = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.path)
       if uri.scheme == 'https'
        require 'net/https'
        conn.use_ssl = true
              conn.verify_mode = OpenSSL::SSL::VERIFY_NONE   # needed for windows environment
      end
    request.body=post_xml
    request.content_type = 'text/xml'
    response = conn.request(request)

    peg_pay_status_code = response.read_body
    peg_pay_status_code