Я использую этот Gem для веб-сервисов Fedex и, действительно, новый для Ruby/Rails, мне нужна помощь в том, как начать использовать этот камень в моем приложении.
Это то, что я получил до сих пор...
test.html.erb
<h4>Shipper</h4>
<div class="col-xs-2">
<input type="text" class="form-control" placeholder="Address">
<input type="text" class="form-control" placeholder="City">
<input type="text" class="form-control" placeholder="State">
<input type="text" class="form-control" placeholder="Postal Code">
</div>
<h4>Recipient</h4>
<div class="col-xs-2">
<input type="text" class="form-control" placeholder="Address">
<input type="text" class="form-control" placeholder="City">
<input type="text" class="form-control" placeholder="State">
<input type="text" class="form-control" placeholder="Postal Code">
</div>
<%= button_to "Generate Rate" %
<h4>Total Cost</h4>
<%= "Output Here" %>
Теперь со входами для сбора адреса отправителя и получения, мне нужно знать, запускать запрос на api через следующий скрипт.
test_helper.rb
require 'fedex'
def fedex
test_creds = {...mycreds...}
fedex_test = Fedex::Shipment.new(test_creds)
recipient = {:name => "Recipient",
:company => "Company",
:phone_number => "555-555-5555",
:address => "1211 Burke Street",
:city => "Cambridge",
:state => "CA",
:postal_code => "02141",
:country_code => "MA",
:residential => "false" }
shipper = { :name => "Bob Jones",
:company => "Test Company",
:phone_number => "777-777-7777",
:address => "8799 W. Music Ln",
:city => "Netherland",
:state => "AD",
:postal_code => "89045",
:country_code => "US" }
packages = []
packages << {
:weight => {:units => "LB", :value => 6},
:dimensions => {:length => 26, :width => 9, :height => 2, :units => "IN" }
}
shipping_options = {
:packaging_type => "YOUR_PACKAGING",
:drop_off_type => "REGULAR_PICKUP"
}
rates = fedex_test.rate({:shipper=>shipper, :recipient => recipient, :packages => packages, :service_type => "FEDEX_GROUND", :shipping_options => shipping_options})
rates.to_s
Я пытаюсь выяснить, как легко связывать входы html "формы" и их значения, которые вводятся с хешем в коде ruby. Я хочу, чтобы это было вызвано нажатием кнопки.
Любая помощь, ссылки на помощь, предложения, примеры кода очень полезны. заранее спасибо
Если я правильно вас понимаю, я бы использовал модель ActiveModel, которая бы инкапсулировала все необходимые атрибуты для расчета ставок Fedex. Эта модель тогда применима в помощнике формы.
Что-то в этом роде. Примечание: вы можете придумать лучшее именование :)
# models/fedex_rates.rb
require 'fedex'
class FedexRates
include ActiveModel::Model
attr_accessor :shippper_address, :shippper_city, :shippper_state, :shippper_postal_code
attr_accessor :recipient_address, :recipient_city, :recipient_state, :recipient_postal_code
def perform
recipient = {
name: recipient_name
company: recipient_company
address: recipient_address
city: recipient_city
state: recipient_state
}
shipper = {
# Set attributes
}
# ... Code to set up packages.
fedex_test.rate({ shipper:shipper, recipient: recipient })
end
end
Создайте соответствующий вид, используя метод rails form_for helper.
# views/fedex_rates/news.html.erb.
<%= form_for @fedex_rates do |f| %>
<%= f.text_field :recipient_address %>
<%= f.text_field :recipient_city %>
<%= f.text_field :recipient_state %>
<%= f.text_field :recipient_postal_code %>
<%= f.text_field :shipper_address %>
<%= f.text_field :shipper_city %>
<%= f.text_field :shipper_state %>
<%= f.text_field :shipper_postal_code %>
<%= end %>
Внедрите контроллер.
# controllers/fedex_rates_controller.rb
class FedexRates < ApplicationController
def new
@fedex_rates = FedexRates.new
end
def create
fedex_rates = FedexRates.new(params[:fedex_rates])
rates = fedex_rates.perform
# Do something with the rates
end
end
Добавить маршруты
# routes
resources :fedex_rates, only: [:new, :create]