mardi 8 décembre 2015

Post JSON product data from Angular to Rails LineItem controller

I have a calculator (to calculate some counts of product and additional positions), written on AngularJS, I am using gem "angular-rails-resources".

I want to pass data from calculator to LineItem (for cart) controller in rails.

    productCenter.factory('LineItem', ['railsResourceFactory', function       (railsResourceFactory) {
    return railsResourceFactory({
        url: '/line_items',
        name: 'line_item'
       });
     }]);


    LineItem.query().then(function (results) {
        $scope.line_items = results;
    });


    $scope.addLineItem = function() {
     new LineItem ({
        product_id: $scope.selectedProduct.id,
      # whole_count: $scope.wholeCount,
     }).create()
    }

In rails i have "create" method from Agile Web book:

def create
  product = Product.find(params[:product_id])
  @line_item = @cart.add_product(product.id)
end

In rails routes:

post "line_items/:product_id" => "line_items#create"

And when I click on "Add to cart" button, in console i have error:

Started POST "/line_items" for ::1 at 2015-12-05 02:45:35 +0300
Processing by LineItemsController#create as JSON
  Parameters: {"line_item"=>{"product_id"=>2}, :product_id=>{}}
  Cart Load (0.0ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 27]]
  Product Load (0.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", nil]]
Completed 404 Not Found in 8ms (ActiveRecord: 0.0ms)

ActiveRecord::RecordNotFound (Couldn't find Product with 'id'={}):
  app/controllers/line_items_controller.rb:41:in `create'

If I change find params in "create" method in LineItem controller this way:

def create
    product = Product.find(product_params)
    @line_item = @cart.add_product(product.id)
 private
   def product_params
      params.require(:line_item).permit(:product_id)
   end
end

In console I have next error:

Started POST "/line_items" for ::1 at 2015-12-05 02:46:22 +0300
Processing by LineItemsController#create as JSON
  Parameters: {"line_item"=>{"product_id"=>2}, :product_id=>{}}
  Cart Load (0.0ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 27]]
  Product Load (0.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", nil]]
Completed 404 Not Found in 26ms (ActiveRecord: 1.0ms)

ActiveRecord::RecordNotFound (Couldn't find Product with 'id'={"product_id"=>2}):
  app/controllers/line_items_controller.rb:41:in `create'

In short: I need to pass many calculated information in angular like: count, consumables_count, instruments_count. And this is why I am using angular and json. But:

  1. I can not find product by id in rails LineItem controller (product = Product.find(product_params) - ERROR)

  2. And I don't know how to pass calculated information to the rails controller, and how to handle it, that I could view this calculated information in cart? (e.g. @cart.line_items.whole_count)

Aucun commentaire:

Enregistrer un commentaire