【问题标题】:Rails: Unpermitted parameter with using nested attributesRails:使用嵌套属性的未经允许的参数
【发布时间】:2016-06-24 07:10:59
【问题描述】:

Unpermitted parameter 在我尝试创建新数据时显示。 虽然有很多类似的问题,我不知道如何解决。

日志

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

如果您能给我任何建议,我们将不胜感激。

【问题讨论】:

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


    【解决方案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

    /schedules/ _schedule_form.html.erb 中将f.fields_for(:room) 更改为f.fields_for(:rooms)

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

    【讨论】:

    • 感谢您的快速回答,@Deepak。对不起,我忘了输入我的强参数。我将它添加到控制器中。你能再检查一下吗?
    • @SamuraiBlue 那么问题出在你的表单上,因为你应该收到rooms_attributes 的 oarams,而你不是。尝试更改f.fields_for(:rooms)
    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多