0

Unpermitted parameter在我嘗試創建新數據時顯示。 Althogh有很多類似的問題,我無法找到如何解決。Rails4:使用嵌套屬性的未經許可的參數

我想要做的是在創建schedule時將值保存在amounts表中。

日誌

Processing by SchedulesController#create as HTML 
    Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test title", "departure_date"=>"2016-07-06", "rooms_attributes"=>{"0"=>{"schedule_id"=>"", "amounts_attributes"=>{"0"=>{"schedule_id"=>"", "room_id"=>""}}}}}, "commit"=>"Create my schedule"} 
    Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test title", "departure_date"=>"2016-07-06", "rooms_attributes"=>{"0"=>{"schedule_id"=>"", "amounts_attributes"=>{"0"=>{"schedule_id"=>"", "room_id"=>""}}}}}, "commit"=>"Create my schedule"} 
    User Load (120.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] 
    User Load (120.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] 
Unpermitted parameter: amounts_attributes 

模式

schedule.rb

class Schedule < ActiveRecord::Base 
    belongs_to :user 
    has_many :rooms, inverse_of: :schedule, dependent: :destroy 
    has_many :amounts, inverse_of: :schedule, dependent: :destroy 
    accepts_nested_attributes_for :rooms, allow_destroy: true 
    accepts_nested_attributes_for :amounts, allow_destroy: true 

rooms.rb

class Room < ActiveRecord::Base 
    belongs_to :schedule, inverse_of: :rooms 
    has_many :events, inverse_of: :room, dependent: :destroy 
    has_many :amounts, inverse_of: :room, dependent: :destroy 
    accepts_nested_attributes_for :events, allow_destroy: true 
    accepts_nested_attributes_for :amounts, allow_destroy: true 

amount.rb

class Amount < ActiveRecord::Base 
    belongs_to :schedule, inverse_of: :amounts 
    belongs_to :room, inverse_of: :amounts 
    belongs_to :event, inverse_of: :amounts 
end 

控制器

schedule_controller.rb

def new 
    @schedule = Schedule.new 
    room = @schedule.rooms.build 
    room.amounts.build 
    end 

    def create 
    @schedule = current_user.schedules.build(schedule_params) 
    if @schedule.save 
     flash[:success] = "schedule created!" 
     redirect_to schedule_path(@schedule) 
    else 
     render 'new' 
    end 
    end 

... 

    def schedule_params 
    params.require(:schedule).permit(
     :title, :departure_date, 
     rooms_attributes: [ 
     :id, :_destroy, :room, :room_address, :schedule_id, :day, 
     amounts_attributes: [ 
      :schedule_id, :room_id, :event_id, :id, :_destroy, :ccy, :amount 
     ] 
     ] 
    ) 
    end 

視圖

/schedules/new.html.erb

... 
<%= form_for(@schedule) do |f| %> 
    <%= render 'schedule_form', f: f %> 
    <%= f.submit "Create my schedule", class: "btn btn-primary" %> 
... 

/日程安排/ _schedule_form.html.erb

它加入4線<%= room.fields_for(:amounts) do |amount| %>以前一樣工作。

... 
    <%= f.label :title %> 
    <%= f.text_field :title, class: 'form-control' %> 
... 

<%= f.fields_for(:rooms) do |room| %> 
    <%= room.hidden_field :schedule_id %> 

    <%= room.fields_for(:amounts) do |amount| %> # my app works before add these 4 lines. 
    <%= amount.hidden_field :schedule_id %> # 
    <%= amount.hidden_field :room_id %>  # 
    <% end %>         # 

<% end %> 

如果您能給我任何建議,我們將不勝感激。

+0

您沒有:您的強制參數中的schedule_id用於Amount_attributes,但您將其作爲參數傳遞 – hypern

+0

感謝您的評論@hypern。我未能複製和粘貼。我更新'schedule_controller.rb'中的強參數。 – SamuraiBlue

回答

0

您還沒有關閉room_attributes數組參數。請關閉它並打開金額的屬性嵌套參數。還有金額屬性中的schedule_id。

def schedule_params 
     params.require(:schedule).permit(:title, 
     :departure_date,{rooms_attributes: [:id, :_destroy, :room, :room_address, :schedule_id, :day, 
     { amounts_attributes: [:itinerary_id, :room_id, :event_id, :id, :_destroy, :ccy, :amount]} 
     ]} 
     ) 
end 
+0

謝謝你的回答,@Sravan。雖然我嘗試了您的答案,但會顯示以下錯誤。 'SyntaxError(/home/ubuntu/workspace/app/controllers/schedules_controller.rb:83:syntax error,unexpected']',期待')'' – SamuraiBlue

+0

現在你的問題解決了嗎?其實我誤解了這個問題。現在我正在改變答案 – Sravan

+0

不,它沒有解決。我仍然有同樣的錯誤'不允許的參數:Amount_attributes'。 – SamuraiBlue

0

從記錄PARAMS以下,你的amounts_attributes是對象的數組,並在您permit方法參數應被聲明爲這樣:

def schedule_params 
    params.require(:schedule).permit(
     :title, :departure_date, 
     rooms_attributes: [{ 
     :id, :_destroy, :room, :room_address, :schedule_id, :day, 
     amounts_attributes: [{ 
      :itinerary_id, :room_id, :event_id, :id, :_destroy, :ccy, :amount 
     }] 
     }] 
    ) 
end 

rooms_attributes如下相似的結構和需要被校正爲好。

查看ActionController::Parameters docs中的嵌套參數示例。

+0

謝謝你的回答,@Nic Nilov。當我添加大括號時,顯示語法錯誤。 – SamuraiBlue

0

我覺得你應該PARAMS是這樣的:

def schedule_params 
    params.require(:schedule).permit(
     :title, :departure_date, 
     rooms_attributes: [ 
     :id, :_destroy, :room, :room_address, :schedule_id, :day, 
     amounts_attributes: [ :id, :_destroy, 
      :itinerary_id, :room_id, :event_id, :id, :_destroy, :ccy, :amount] 
] 
) 
end 

因此,對於您的嵌套模式,即使是深度嵌套,仍然需要:身份證和:_destroy。

讓我知道這是怎麼回事。

+0

謝謝你的回答,@nilatti。當我將'amount_attributes'改爲如下時,'Unpermitted parameter'消失。 'amount_attributes:[:id,:_destroy,:itinerary_id,:room_id,:event_id,:ccy,:amount]'。我已經在'amount_attributes'中有':id,:_destroy'。所以我改變了這兩個位置。但'itinerary_id'尚未保存。你有什麼想法嗎? – SamuraiBlue

+0

這可能聽起來很傻,但我沒有看到你的表單在哪裏設定行程。 – nilatti

+0

感謝您的評論,@nilatti。我很抱歉我犯了錯字。我把'itinerary_id'改成''amount_attributes'中的'schedule_id'。雖然'schedule_id'也用於'rooms_attributes',它可以保存在rooms表中。 – SamuraiBlue