[英]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". 我有一个用AngularJS编写的计算器(用于计算产品和其他职位的数量),我使用的是宝石“ angular-rails-resources”。
I want to pass data from calculator to LineItem (for cart) controller in rails. 我想将数据从计算器传递到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()
}
In rails i have "create" method from Agile Web book: 在Rails中,我有敏捷Web手册中的“ create”方法:
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: 如果我以这种方式更改LineItem控制器中“ create”方法中的find参数:
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. 简而言之:我需要按角度传递许多计算的信息,例如:count,consumables_count,instruments_count。 And this is why I am using angular and json. 这就是为什么我使用angular和json的原因。 But: 但:
I can not find product by id in rails LineItem controller (product = Product.find(product_params) - ERROR) 我无法在Rails LineItem控制器中通过ID查找产品(产品= Product.find(product_params)-错误)
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? 而且我不知道如何将计算的信息传递给rails控制器以及如何处理它,以便我可以在购物车中查看此计算的信息? (eg @cart.line_items.whole_count) (例如@ cart.line_items.whole_count)
When you do this: 执行此操作时:
product = Product.find(product_params)
You pass {"product_id" => 2}
as parameter for find
. 您将{"product_id" => 2}
传递为find
参数。 This is not correct, instead you should do like this: 这是不正确的,相反,您应该这样做:
product = Product.find(product_params[:product_id])
find
accepts only identifier of a record in DB. find
仅接受数据库中记录的标识符。
Finally, I code this way: 最后,我这样编码:
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 and product_attr_params => should be merge product_params和product_attr_params =>应该合并
My POST from AngularJS 我从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()
And LineItem model: 和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.