Rails:使用嵌套属性的未允许参数

时间:2016-06-24 07:10:59

标签: ruby-on-rails ruby-on-rails-4

我尝试创建新数据时显示

Unpermitted parameter。 Althogh有很多类似的问题,我找不到如何解决。

登录

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" %>
...

/ schedules / _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 个答案:

答案 0 :(得分:1)

你没有以正确的格式获得参数

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

您的参数应包含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 %>