1
votes

I'm using the Shopify API http://api.shopify.com/

And the Shopify Gem: https://github.com/Shopify/shopify_api that does most of the heavy lifting- just can't quite figure out how to make it work.

To update an @variant object I need to PUT here: PUT /admin/variants/#{id}.json

In config/routes.rb I made default resource routes with resources :variants and now I'm trying to make a form that updates a variant resource but can't configure the form to have the proper action.

Basically I'm constructing form_tag with a text field input that takes an integer and updates variant.inventory_quantity

Rake Routes give me this:

rake routes: 

variants     GET    /variants(.:format)           variants#index
             POST   /variants(.:format)           variants#create
new_variant  GET    /variants/new(.:format)       variants#new
edit_variant GET    /variants/:id/edit(.:format)  variants#edit
variant      GET    /variants/:id(.:format)       variants#show
             PUT    /variants/:id(.:format)       variants#update
             DELETE /variants/:id(.:format)       variants#destroy
1

1 Answers

0
votes

You need to declare variants resource under admin namespace like this:

config/routes.rb

namespace :admin do
  resources :variants
end

EDIT:

You don't have to do anything special for Rails to accept JSON. Rails will convert the JSON you passed in PUT into params and make it available to update method.

Here is the standard implementation of 'update' method:

app/controllers/admin/variants_controller.rb

def update
  @variant = Variant.find(params[:id])
 
  respond_to do |format|
    if @variant.update_attributes(params[:variant])
      format.html  { redirect_to(@variant,
                    :notice => 'Variant was successfully updated.') }
      format.json  { head :no_content }
    else
      format.html  { render :action => "edit" }
      format.json  { render :json => @variant.errors,
                    :status => :unprocessable_entity }
    end
  end
end

Refer to Rails guide and layout and rendering for details.