mardi 23 juin 2015

Rails: def create does not add data to table using Rest

I want to add data to table through rest api with url: localhost:3000/api/v1/shoppinglists#create?grocery=fruits

I have created a model already, my controller is located under api/v1/shoppinglists_controller.rb and the code for that is:

shoppinglists_controller.rb:

module Api
    module V1

        class ShoppinglistsController < ApplicationController

            def index
                shop = Shoppinglist.all
                render json: shop.to_json
            end

            def create
                @tst = Shoppinglist.create(grocery: "params[:grocery]")
            end
        end
    end
end

routes.rb:

Rails.application.routes.draw do

  namespace :api do
    namespace :v1 do
      get '/shoppinglists' => 'shoppinglists#index'
      post '/shoppinglists' => 'shoppinglists#create'
    end
  end
end

By default, def index gets triggered but when I do: localhost:3000/api/v1/shoppinglist#create?grocery=fruits then I check on commandline, I still see:

Started GET "/api/v1/shoppinglists" for ::1 at 2015-06-23 23:59:06 -0400
Processing by Api::V1::ShoppinglistsController#index as HTML
  Shoppinglist Load (0.3ms)  SELECT "shoppinglists".* FROM "shoppinglists"
Completed 200 OK in 44ms (Views: 0.2ms | ActiveRecord: 0.4ms)

and my table is empty. I dont understand why still index is getting triggered and how can I make def create to actually insert value in grocery column through rest api.

Aucun commentaire:

Enregistrer un commentaire