Go back to the main page

Get started integrating Rails 4 and QuickBooks Online with the Quickeebooks Gem: Part 2


Part 2 aim

Create and update a vendor on both the Rails app and QB0.

  1. Add an email_address attribute to the Vendor.
  2. $ rails g migration add_email_address_to_vendors email_address
    $ bundle exec rake db:migrate
  3. Add email_address to the views.
    • app/views/vendors/_form.html.erb
    • ... omitted
      <div class="field">
          <%= f.label :email_address %><br>
          <%= f.email_field :email_address %>
      ... omitted
    • app/views/vendors/index.html.erb
    •    ... omitted
            <th>Email address</th>
          <% @vendors.each do |vendor| %>
            ... omitted
              <td><%= vendor.email_address %></td>
              <td><%= link_to 'Show', vendor %></td>
              <td><%= link_to 'Edit', edit_vendor_path(vendor) %></td>
              <td><%= link_to 'Destroy', vendor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
          <% end %>
        ... omitted
    • app/views/vendors/show.html.erb
    • ... omitted
        <strong>Email address:</strong>
        <%= @vendor.email_address %>
      ... omitted
  4. Reload browser at billtastic.dev/vendors and test that the views have been updated.
  5. Create a new vendor locally and within QBO.
    • Add a before_action filter within app/controllers/vendors_controller.rb to activate the Quickeebooks vendor service.
      class VendorsController < ApplicationController
        before_action :set_vendor, only: [:show, :edit, :update, :destroy]
        before_action :set_qb_service, only: [:create, :edit, :update, :destroy]
      #... omitted
      #... omitted
          def set_qb_service
            oauth_client = OAuth::AccessToken.new($qb_oauth_consumer, session[:token], session[:secret])
            @vendor_service = Quickeebooks::Online::Service::Vendor.new
            @vendor_service.access_token = oauth_client
            @vendor_service.realm_id = session[:realm_id]
      DISCUSSION: Put before_action after the set_vendor filter, which was created by the scaffolding generator in Part 1. The set_qb_service method should be private and the very last method of the class.
    • Whitelist the email_address attribute within the vendors_controller.
    •     def vendor_params
            params.require(:vendor).permit(:name, :email_address)
    • Modify the create action within the vendors_controller.
    •   def create
          @vendor = Vendor.new(vendor_params)
          vendor = Quickeebooks::Online::Model::Vendor.new
          vendor.name = vendor_params[:name]
          vendor.email_address = vendor_params[:email_address]
          respond_to do |format|
            if @vendor.save
              format.html { redirect_to @vendor, notice: 'Vendor was successfully created.' }
              format.json { render action: 'show', status: :created, location: @vendor }
              format.html { render action: 'new' }
              format.json { render json: @vendor.errors, status: :unprocessable_entity }
  6. Go to the browser and refresh billtastic.dev/vendors and then click New vendor link.
    Fill in these fields and click 'Create Vendor'.
  7. Go over to QBO and check to see that the vendor was created.
  8. Navigate to the 'Company' tab and then to the 'Activity Log' submenu. Also, click on the vendor's name within the activity tab to get more details.
    Great, we can see that the email address was also created.
  9. Now let's change the email address to exercise Quickeebooks' update functionality. Go back to the browser and click 'Edit'.
  10. After you get to the edit view stop as we are going add some code the vendors_controller.rb.
  11. Modify the update action within the vendors_controller.
  12.   def update
        respond_to do |format|
          if @vendor.update(vendor_params)
            vendor = @vendor_service.list.entries.find{ |e| e.name == vendor_params[:name] }
            vendor.email_address = vendor_params[:email_address]
            format.html { redirect_to @vendor, notice: 'Vendor was successfully updated.' }
            format.json { head :no_content }
            format.html { render action: 'edit' }
            format.json { render json: @vendor.errors, status: :unprocessable_entity }
    DISCUSSION: The first highlighted line is searching your QBO vendor entries that match the submitted name and then returning the vendor. Notice in the next line that I only have the email_address as an update-able attribute and not name. This is because the name attribute for a Vendor (and other QBO entities such as Employee and Customer) is a unique attribute and can't be modified.
  13. Let's go back to the browser, which should be at the url billtastic.dev/vendors/1/edit.
  14. Change the email address and click 'Update Vendor'.
  15. Go back to QBO and check to see that the update was successful.
  16. Click on the vendor's name to see if the email address was updated.
    Yay! The email address update worked!

Finished Next

This concludes a 2-part tutorial where a QuickBooks Online entity was created and updated from a Rails app. I sincerely hope this series aids your ramp up time in getting integrated with QuickBooks Online. Of course, don't hesitate to point out errors or ask for clarification within the article's comments. This tutorial continues with Part 3 that demonstrates disconnecting from QBO.