【发布时间】:2016-01-28 15:39:50
【问题描述】:
我需要一个熟悉 Rails、AngularJS 和 Restangular 的人,我正在尝试通过 AngularJS 从 Rails 更新模型,但我对正在发送的参数感到复杂......
我在 AngularJS 控制器中使用这个函数首先捕获表单中的数据(分配给 poll_updated 变量的数据),然后将更新请求发送到 Rails...
$scope.editPoll = function() {
var poll_updated = {title: $scope.title, description: $scope.description, allow_anonymous_answer: $scope.allow_anonymous_answer, initial_message: $scope.initial_message, final_message: $scope.final_message};
console.log(poll_updated);
Restangular.one('polls', poll.id).put(poll_updated).then(function(poll_updated) {
$state.go('add_data_poll', poll_updated);
});
};
请注意,poll.id 是要修改的投票的 ID,我通过控制台打印 poll_updated(假设是新数据)。
这是我在谷歌浏览器控制台中得到的...
绝对正确,这些是我发送的参数...
现在我给你看 Rails 的日志
在第一个矩形中我们仍然可以看到发送的参数仍然是正确的(我可以说id也是正确的),但问题出在第二个矩形...这里我显示参数poll_params 用于更新模型,它只包含 ID...
这是 Rails 的 polls_controller 中更新操作的代码...
def update
puts "<(PARAMETERS)>: " + poll_params.to_s
respond_to do |format|
if @poll.update(poll_params)
format.html { redirect_to @poll, notice: 'Poll was successfully updated.' }
format.json { render :show, status: :ok, location: @poll }
else
format.html { render :edit }
format.json { render json: @poll.errors, status: :unprocessable_entity }
end
end
end
我做了几次测试,唯一得出的结论是,唯一的问题是poll_params没有得到我发送的所有参数,只有id。
我该如何解决这个问题?
这些是我允许更新的参数...
def poll_params
params.require(:poll).permit(:user, :title, :description, :allow_anonymous_answer, :initial_message, :final_message)
end
那么问题出在哪里?
【问题讨论】:
-
基本上,你看到
poll: { id: '..')了吗,你在poll根键里面什么都没有,你需要把所有的数据包装在那里..看看如何通过阅读@包装这个987654323@ 。让我知道你是否仍然卡在那里
标签: javascript ruby-on-rails angularjs json