【问题标题】:Rails4: Unpermitted parameter with using nested attributesRails4:使用嵌套属性的未经允许的参数
【发布时间】:2016-07-06 08:23:06
【问题描述】:

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

我想做的是在创建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

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

/schedules/_schedule_form.html.erb

它在为&lt;%= room.fields_for(:amounts) do |amount| %&gt; 添加 4 行之前有效。

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

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

【问题讨论】:

  • 您的 amount_attributes 的强参数中没有 :schedule_id,但您将其作为参数传递
  • 感谢您的评论,@hypern。我无法复制和粘贴。我更新了schedule_controller.rb中的强参数。

标签: ruby-on-rails ruby-on-rails-4 nested-attributes fields-for


【解决方案1】:

您还没有关闭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 

【讨论】:

  • 感谢您的回答,@Sravan。尽管我尝试了您的答案,但仍显示以下错误。 SyntaxError (/home/ubuntu/workspace/app/controllers/schedules_controller.rb:83: syntax error, unexpected ']', expecting ')'
  • 您的问题现在解决了吗?其实我误解了这个问题。现在我正在改变答案
  • 不,它不能解决。我仍然有同样的错误Unpermitted parameter: amounts_attributes
  • 我更新了答案,请检查,它不起作用,那么错误应该在关联或表单中。
【解决方案2】:

根据记录的参数,您的 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中的嵌套参数示例。

【讨论】:

  • 感谢您的回答,@Nic Nilov。添加大括号时显示语法错误。
【解决方案3】:

我认为你的参数应该是这样的:

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

因此,对于您的嵌套模型,即使嵌套很深,您仍然需要 :id 和 :_destroy。

让我知道这对你有什么好处。

【讨论】:

  • 感谢您的回答,@nilatti。当我将amounts_attributes 更改为以下内容时,Unpermitted parameter 消失了。 amounts_attributes: [:id, :_destroy, :itinerary_id, :room_id, :event_id, :ccy, :amount]。我已经在amounts_attributes 中有:id, :_destroy,。所以我改变了这两个的位置。但是itinerary_id 还没有保存。你有什么想法吗?
  • 这听起来可能有点傻,但我看不出您在表单的哪个位置设置了行程。
  • 感谢您的评论,@nilatti。对不起,我打错了。我在amounts_attributes 中将itinerary_id 更改为schedule_id。虽然schedule_id也用在rooms_attributes中,但可以保存在rooms表中。
猜你喜欢
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 2017-06-03
  • 2016-02-06
  • 1970-01-01
  • 2013-07-22
相关资源
最近更新 更多