2016-11-24 11 views
0

Ajax呼び出しで文書と埋め込み文書を提出しようとしていますが、「許可されていないパラメータ」例外を受け取り続けます。 これは私のモデルである:ネストされた属性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

はあなたのすべてをありがとう!

答えて

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', 

は、私が(:文字列 『2016年11月25日』のための未定義のメソッド `with_indifferent_access'):*のNoMethodErrorに走った *エラーに起因しているその時のエントリ値は配列である必要があります。

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

これは機能します。

関連する問題