0

我正在尝试通过 Ajax 调用提交文档和嵌入文档,但一直收到“未经允许的参数”异常。这是我的模型:

class UserForecast
...
  embeds_many :time_entries
  accepts_nested_attributes_for :time_entries
...
end

我的强大参数:

def user_forecast_params
  params.require(:user_forecast).permit(:published, :user_id, :forecast_id, :project_role_id, time_entries_attributes: [:entry_date, :hours])
end

ajax 调用:

$.ajax({
    url : '/user_forecasts.json' ,
    data : { user_forecast: { user_id: timeEntry.data('user_id'), forecast_id: timeEntry.data('forecast_id'), project_role_id: timeEntry.data('project_role_id'), time_entries: { entry_date: timeEntry.data('date'), hours: timeEntry.value } }},
    type : 'post',
...

我看不到我遗漏的任何东西,但我在日志中收到了这个:

Unpermitted parameter: time_entries

我正在使用:Ruby 2.3.0 Rails:4.2.6 Mongoid:5.1.5

谢谢你们!

4

1 回答 1

0

好的,我想通了。

首先:显然,JSON 参数名称需要是time_entries_attributes:而不是 time_entries。

$.ajax({
    url : '/user_forecasts.json' ,
    data : { user_forecast: { user_id: timeEntry.data('user_id'), forecast_id: timeEntry.data('forecast_id'), project_role_id: timeEntry.data('project_role_id'), time_entries_attributes: [{ entry_date: timeEntry.data('date'), hours: timeEntry.value }] }},
    type : 'post',

越过那座桥后,我遇到了 *NoMethodError (未定义的方法 `with_indiffer_access' for "2016-11-25":String): * 错误,这是因为时间条目值需要是一个数组:

[{ entry_date: timeEntry.data('date'), hours: timeEntry.value }]

这行得通!

于 2016-11-24T16:56:40.657 回答