2016-06-24 158 views
0

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

日誌

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

模式

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 

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 

控制器

schedule_controller.rb

def new 
    @schedule = Schedule.new 
    room = @schedule.rooms.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, 
     events_attributes: [ 
      :id, :_destroy, :start_at, :end_at, :title, :detail, :category,      
      amounts_attributes: [ 
      :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

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

<%= f.fields_for(:room) do |a| %> 
    <%= a.text_field :room %>  #room column exist in the rooms table 
<% end %> 

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

回答

1

你沒有得到的PARAMS以適當的格式

Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test", "departure_date"=>"2016-06-24", "room"=>{"room"=>"test name"}}, "commit"=>"Create my schedule"} 
Unpermitted parameter: room 

您的PARAMS應該包含rooms_attributes代替room

變化f.fields_for(:room)f.fields_for(:rooms)/schedules/ _schedule_form.html.erb

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

<%= f.fields_for(:rooms) do |a| %> 
    <%= a.text_field :room %>  #room column exist in the rooms table 
<% end %> 
+0

謝謝您的快速回答, @Deepak。對不起,我忘了輸入我的強大參數。我將它添加到控制器中。你能再檢查一次嗎? – SamuraiBlue

+1

@SamuraiBlue然後,問題出現在你的表單中,因爲你應該接受oarams作爲'rooms_attributes',而你不是。 嘗試更改'f.fields_for(:rooms)' –

+0

感謝您的評論,@Deepak。有用! – SamuraiBlue