2

我很难让rails 4使用nested_attributes和序列化。我有:如何使rails strong_parameters + nested_attributes + serialize工作?

class Client < ActiveRecord::Base 
    belongs_to :event 
    serialize :phones 
end 


class Event < ActiveRecord::Base 
    has_one :client 
end 


class EventsController < ApplicationController 

    ... 

    def event_params 
    params.permit(client_attributes: [:phones]) 
    end 
end 

当我通过事件:

{client_attributes: { phones: 'string'}} 

它的工作原理,但是当我尝试

{client_attributes: { phones: [{phone_1_hash},{phone_2_hash}]}} 

得到 '未经许可的参数:手机' 不保存消息和现场...

我试过使用

class EventsController < ApplicationController 

    ... 

    def event_params 
    params.permit(client_attributes: [phones:[]]) 
    end 
end 

class Client < ActiveRecord::Base 
    belongs_to :event 
    serialize :phones, Array 
end 

但至今没有任何帮助。任何建议,将不胜感激。谢谢!

回答

6

Pfff - 终于得到了它......凭借强大的参数没有未知键可以通过,所以这里的解决办法是:

class EventsController < ApplicationController 

    ... 

    def event_params 
    params.permit(client_attributes: [ {phones: [:number, :type]}]) 
    end 
end 

基于http://edgeapi.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit-21

希望它可以帮助别人。

我可以在我的serialisable字段中指定键值,但是用户添加键值是什么?序列化字段可以使用强参数吗? (这可能应该是个新问题......)

相关问题