嵌套属性在Rails的4 +角项目保存到数据库 [英] Nested attributes not saved to database in Rails 4 + Angular project
问题描述
这里涉及的两个模型的项目和库存模型,有一个的has_many:通过=>:分类已彼此之间的关系。也就是说,每一个项目都有属于库存(S),反之亦然。问题在于,在更新该项目时,库存属性不保存到数据库中,而其他的非嵌套属性可适当更新。
你的帮助是非常AP preciated T.T
The two models involved here are the Item and Inventory models, having a "has_many :through => :categorizations" relationship to each other. Namely, each item has and belongs to inventory(s) and vise versa. The problem is that the "inventories" attribute is not saved to database when updating the item, while other non-nested attributes can be properly updated. Your help is much appreciated T.T
item.rb的
class Item < ActiveRecord::Base
has_many :checkouts, :dependent => :destroy
has_many :categorizations
has_many :inventories, :through => :categorizations
accepts_nested_attributes_for :categorizations, :allow_destroy => true, :reject_if => :all_blank
accepts_nested_attributes_for :inventories
end
inventory.rb
inventory.rb
class Inventory < ActiveRecord::Base
has_many :categorizations
has_many :items, :through => :categorizations
accepts_nested_attributes_for :categorizations, :allow_destroy => true, :reject_if => :all_blank
end
items_controller.rb
items_controller.rb
# PUT /items/1
# PUT /items/1.json
def update
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to @item, :notice => 'Item was successfully updated.' }
format.json { respond_with(@item) }
else
format.html { render action: 'edit' }
#format.json { render json: @item.errors, status: :unprocessable_entity }
respond_with(@item)
end
end
end
......
def item_params
params.require(:item).permit!
end
ItemsController.coffee(对于所有模板角控制器)
ItemsController.coffee (the angular controller for all templates)
controllers = angular.module('controllers')
controllers.controller("ItemsController", [ '$scope', '$routeParams', '$location','$resource','Flash','ngDialog'
($scope,$routeParams,$location,$resource,Flash,ngDialog)->
#Item querying
Item = $resource('/api/items/:itemId', { itemId: "@id", format: 'json' },
{
'get': { method: 'GET'},
'save': { method:'PUT'},
'create': { method:'POST'},
'update': { method:'PUT'},
'duplicate': { url: 'api/items/:itemId/duplicate', method:'PUT'},
'checkin': { url: '/api/items/:itemId/checkin', method:'PUT'}
'checkin': { url: '/api/items/:itemId/checkin', method:'GET'},
'checkout': { url: '/api/items/:itemId/checkout', method:'GET'},
'checkout': { url: '/api/items/:itemId/checkout', method:'POST'}
}
)
Item.query().$promise.then ((items) ->
$scope.items = items
), (errResponse) ->
#Inventory querying
Inventory = $resource('/api/inventories/:inventoryId', { inventoryId: "@id", format: 'json' },
{
'get': { method: 'GET'},
'save': {method:'PUT'},
'create': {method:'POST'}
}
)
Inventory.query().$promise.then ((inventories) ->
$scope.inventories = inventories
), (errResponse) ->
$scope.saveItem = ->
onError = (_httpResponse)-> flash.error = "Something went wrong"
if $scope.item.id
$scope.$apply ->
$scope.item.$update({itemId: $scope.item.id}
( ()->
console.log $scope.item.inventories
ngDialog.close()
$scope.openItemView($scope.item)
$scope.warningUpdateItem() ),
onError)
else
Item.create($scope.item,
( (newItem)->
ngDialog.close()
$scope.openItemView(newItem)
$scope.warningCreateItem() ),
onError
)
edit_item.html.erb
edit_item.html.erb
<div class="form-group" ng-class="{'has-warning has-feedback':errors.name}" ng-repeat="inventory in item.inventories">
<label for="inventories">
Inventory
</label>
<input type="text" name="inventories" class="form-control" ng-model='inventory.name' placeholder="Item inventories"></input>
</div>
服务器响应的项目时,编辑库存属性。
Server Response when editing the inventories attributes of an item
Started PUT "/api/items/15?format=json" for ::1 at 2015-03-26 16:14:54 -0400
Processing by ItemsController#update as JSON
Parameters: {"id"=>"15", "name"=>"arashi", "quantity"=>1, "notes"=>"", "tags"=>"岚", "price"=>23, "purchase_date"=>nil, "needs_repair"=>false, "repair_notes"=>"", "is_disposed"=>false, "disposed_on"=>nil, "is_out"=>nil, "created_at"=>"2015-03-26T13:17:36.000Z", "updated_at"=>"2015-03-26T19:48:21.000Z", "warranty_expires_on"=>nil, "inventories"=>[{"id"=>1007, "name"=>"Groups", "created_at"=>"2015-03-26T13:17:36.000Z", "updated_at"=>"2015-03-26T13:17:36.000Z"}], "item"=>{"id"=>"15", "name"=>"arashi", "quantity"=>1, "notes"=>"", "tags"=>"岚", "price"=>23, "purchase_date"=>nil, "needs_repair"=>false, "repair_notes"=>"", "is_disposed"=>false, "disposed_on"=>nil, "is_out"=>nil, "created_at"=>"2015-03-26T13:17:36.000Z", "updated_at"=>"2015-03-26T19:48:21.000Z", "warranty_expires_on"=>nil}}
Can't verify CSRF token authenticity
Item Load (0.3ms) SELECT `items`.* FROM `items` WHERE `items`.`id` = 15 LIMIT 1
(0.1毫秒)BEGIN
(0.1毫秒),凯明
完成204 17MS无内容(ActiveRecord的:为0.5ms)
(0.1ms) BEGIN (0.1ms) COMMIT Completed 204 No Content in 17ms (ActiveRecord: 0.5ms)
推荐答案
只是为了让您的PARAMS更容易阅读我重新格式化它们:
Just to make your params easier to read I reformatted them:
{"id"=>"15",
"name"=>"arashi",
"quantity"=>1,
"notes"=>"",
"tags"=>"岚",
"price"=>23,
"purchase_date"=>nil,
"needs_repair"=>false,
"repair_notes"=>"",
"is_disposed"=>false,
"disposed_on"=>nil,
"is_out"=>nil,
"created_at"=>"2015-03-26T13:17:36.000Z",
"updated_at"=>"2015-03-26T19:48:21.000Z",
"warranty_expires_on"=>nil,
"inventories"=>
[{"id"=>1007,
"name"=>"Groups",
"created_at"=>"2015-03-26T13:17:36.000Z",
"updated_at"=>"2015-03-26T13:17:36.000Z"}],
"item"=>
{"id"=>"15",
"name"=>"arashi",
"quantity"=>1,
"notes"=>"",
"tags"=>"岚",
"price"=>23,
"purchase_date"=>nil,
"needs_repair"=>false,
"repair_notes"=>"",
"is_disposed"=>false,
"disposed_on"=>nil,
"is_out"=>nil,
"created_at"=>"2015-03-26T13:17:36.000Z",
"updated_at"=>"2015-03-26T19:48:21.000Z",
"warranty_expires_on"=>nil}}
所以,现在你可以看到库存
为您的PARAMS(所以在你的控制器的顶层数组会在 PARAMS [:存货]
),这就是所谓库存
So now you can see that "inventories"
is at the top level of your params (so in your controller that array would be at params[:inventories]
) and it's called "inventories"
有关 accepts_nested_attributes_for
,什么Rails的预计是该阵列是在 PARAMS [:项目] [:inventories_attributes]
For accepts_nested_attributes_for
, what Rails expects is for that array to be in params[:item][:inventories_attributes]
现在,那里恰巧关于这事的问题轨道-core邮件列表这一周,指着 AngularJS Rails的资源<答案上/ A>其中,除其他好吃的东西,有做这些参数的正确序列化 nestedAttribute
方法。
Now, there happened to be a question about this very thing on the rails-core mailing list this week, and an answer pointing to AngularJS Rails Resource which, amongst other goodies, has a nestedAttribute
method for doing the correct serialization of these parameters.
这篇关于嵌套属性在Rails的4 +角项目保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!