If have a simple Rails controller defined like so:
class ProductsController < ApplicationController
respond_to :json
def index
end
def new
end
def create
@product = Product.new(product_params)
@product.save
redirect_to @product
# this can be used if there is no view already created
# render plain: params[:products].inspect
end
def show
@product = Product.find(params[:id])
respond_with @product
end
def show_all
# @products = Product.all
# respond_with @products
respond_to do |format|
@products = Product.all
format.html
format.json { render json: @products }
end
end
private
def product_params
params.require(:product).permit(:name, :description)
end
end
I have attempted to use this in my Angular service by doing the following:
function HomeService($resource) {
function exposeTest() {
/*
{
'create': { method: 'POST' },
'index': { method: 'GET', isArray: true },
'show': { method: 'GET', isArray: false },
'update': { method: 'PUT' },
'destroy': { method: 'DELETE' }
}
*/
return $resource('/products/show_all', {}, {
'show_all': { method: 'GET', isArray: false }
});
}
return {
exposeTest: exposeTest
};
}
And I call this in my home.controller.js like so:
console.log('Expose Test: '+JSON.stringify(HomeService.exposeTest().show_all()));
It's my understanding that you can define a $resource object to interact with the controller - i.e. I have defined a GET type method which can be called with show_all but all I get back is an empty object.
What am I doing wrong?
Thanks
console.logis executed right away (synchronous)