我正在使用angular rails资源开发一个angular应用程序w/rails。当用户更新一张照片时,我想查询关联并将属性更新为true
。
我已经通读了Angular Rails资源文档,但是我没有看到太多关于查询关联的信息。这是一个有角度的东西,还是一个轨道的东西?
下面是我的angular代码:$scope.uploadHeadshot = ->
Upload.upload(
url: window.apiUrl + '/personas/' + $routeParams.unique_code + '/' + $routeParams.slug
method: 'PUT'
data: persona: headshot_attributes:
image: $scope.headshot
crop_x: $scope.cropAttributes.cropImageLeft
crop_y: $scope.cropAttributes.cropImageTop
crop_w: $scope.cropAttributes.cropImageWidth
crop_h: $scope.cropAttributes.cropImageHeight
# right here
badge_attributes: completed: true).then ((response) ->
$mdDialog.hide response.data
resizeImage(false)
return
)
rails代码 def update
@persona = Persona.friendly.find params[:id]
@persona.assign_attributes persona_params
return unprocessable_entity 'Invalid parameters', @persona.errors unless @persona.save
render json: @persona, include: %w(modules owner badges)
end
,在persona_params
中,我有属性badge_attributes: [:id, :completed]
我也有一个attr on徽章的badge_type
,所以从角度来看,我可以得到当前用户,做一个badge.where(badge_type: 'badge type').update(completed: true)
,但我不确定如何从角度做到这一点。或者这是一个rails的东西你可以从#update
if params[:badge_attributes]
Badge.where(badge_type: 'badge type').first.update(completed: true)
end
我所发布的没有做任何事情,但它不会破坏照片上传的发生,我可以看到badge_attribute
参数被发送。有人知道我是怎么做到的吗?
我能够通过使用铁轨进行繁重的工作来解决这个问题。
if params[:badge_attributes]
badge = Badge.where(persona_id: current_persona.id, badge_type: params[:badge_attributes][:badge_type]).first
badge.update_attributes(completed: true)
end