Post JSON 产品数据从 Angular 到 Rails LineItem 控制器

Post JSON product data from Angular to Rails LineItem controller

我有一个计算器(计算产品的一些计数和额外的头寸),写在AngularJS,我正在使用gem "angular-rails-resources"。

我想将数据从计算器传递到 rails 中的 LineItem(用于购物车)控制器。

    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()
    }

在 rails 中,我有 "create" 敏捷网络书中的方法:

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

在rails条路线中:

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

当我点击 "Add to cart" 按钮时,控制台出现错误:

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'

如果我这样更改 LineItem 控制器中 "create" 方法中的查找参数:

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

在控制台中我有下一个错误:

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'

简而言之: 我需要在 angular 中传递许多计算信息,例如:计数、consumables_count、instruments_count。这就是我使用 angular 和 json 的原因。但是:

  1. 我无法在 rails LineItem 控制器中按 ID 找到产品(产品 = Product.find(product_params)- 错误)

  2. 而且我不知道如何将计算的信息传递给 rails 控制器,以及如何处理它,我可以查看这个计算 购物车中的信息? (例如@cart.line_items.whole_count)

当你这样做时:

product = Product.find(product_params)

您将 {"product_id" => 2} 作为 find 的参数传递。这是不正确的,相反你应该这样做:

product = Product.find(product_params[:product_id])

find 只接受数据库中记录的标识符。

最后,我这样编码:

def create
    product = Product.find(product_params[:product_id])
    product_attr = product_attr_params
    @line_item = @cart.add_product(product.id, product_attr)
   end 

private
 def product_attr_params
      params.require(:line_item).permit(:whole_count, :trim_count, :tools_count, :consumables_count, :fill_count, :finish_count, :install_count)
    end 

    def product_params
        params.require(:line_item).permit(:product_id)
    end
end

Product_params 和 product_attr_params => 应该合并

我的 POST 来自 AngularJS

      new LineItem ({
            product_id: $scope.selectedProduct.id,
            whole_count: whole_count,
            trim_count: trim_count,
            tools_count: tools_count,
            consumables_count: consumables_count,
            fill_count: fill_count,
            finish_count: finish_count,
            install_count: install_count,
        }).create()

和 LineItem 模型:

def add_product(product_id, attr)
        current_item = line_items.find_by(product_id: product_id)
        if current_item
            current_item
        else
            current_item = line_items.build(
                product_id: product_id,
                whole_count: attr[:whole_count],
                trim_count: attr[:trim_count],
                tools_count: attr[:tools_count],
                consumables_count: attr[:consumables_count],
                fill_count: attr[:fill_count],
                finish_count: attr[:finish_count],
                install_count: attr[:install_count],
                )
        end
        current_item
    end