嵌套的白名单属性仍未被允许

时间:2015-05-08 19:21:14

标签: ruby-on-rails activerecord nested-forms whitelist

我有三个模型:event,event_user,event_users_day。

事件接受嵌套属性event_user,它也接受event_users_day作为嵌套属性。

class Event < ActiveRecord::Base
  has_many :event_users, :dependent => :destroy, :inverse_of => :event
  accepts_nested_attributes_for :event_users, :allow_destroy => true
end

class EventUser < ActiveRecord::Base
  belongs_to :event, :inverse_of => :event_users
  has_many :event_users_days, :dependent => :delete_all
  accepts_nested_attributes_for :event_users_days, :allow_destroy => true
end

class EventUsersDay < ActiveRecord::Base
  belongs_to :event_users, inverse_of: :event_users_days
  validates :event_users, :presence => true
end

简单的嵌套表单很简单:

= simple_nested_form_for :event_users do |f|
  = f.fields_for :event_users_days do |day|
    = day.input :event_day_id, as: :check_boxes, collection: @daygroups
= f.submit :class => "btn btn-success"

在我的控制器event_user中,event_users_days的属性列入白名单:

    @event_user = EventUser.new(params.permit(:event_id), params[:event_users].permit(:id, event_users_days_attributes: [:id, :event_day_id]))

但是当我保存它时,只保存EventUser,因为服务器告诉我不允许event_users_days:

Unpermitted parameter: event_users_days

关于我做错什么的任何想法?

1 个答案:

答案 0 :(得分:0)

var myMeshes = []; var myMeshHash = {}; function loadMyModel(path, func){ var loader = new THREE.OBJLoader(path); loader.load(path, function(object){ object.name = "path"; object.traverse(function(child){ if(child instanceof THREE.Mesh){ child.material.map = texture; } }); object.position.set(0,0,0); scene.add(object); myMeshes.push(object); myMeshHash[path] = object; if(func != undefined){ func(object); } }); } //use: loadMyModel("models/stuff2.obj", function(mesh){ console.log(mesh); }); 错误是字面意思,因此您的表单生成Unpermitted parameter参数而不是预期的event_users_days参数,Rails正确拒绝该参数。

我很长时间没有使用过event_users_days_attributes,如果你使用的是Rails 4,那么我不确定这将是最好的选择(并且没有必要),但即便如此我认为问题是你使用的是nested_form而不是:event_users - 但通常我建议切换到@event_users,除非你在旧的Rails中(如果你是那么你在SO)提问时应指明。