嵌套的属性不允许通过AJAX嵌入Mongoid文档

问题描述:

我想通过Ajax调用提交文档和嵌入式文档,但不断收到“未经许可的参数”异常。 这是我的模型:嵌套的属性不允许通过AJAX嵌入Mongoid文档

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

谢谢大家!

好吧,我明白了这一点。

第一:显然,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_indifferent_access'为‘2016年11月25日’:字符串): *错误,是由于这样的事实,时间进入值需要是一个数组:

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

This Works!