如何使用best_in_place gem更改序列化数据?

我有一个带有序列化数据的模型,我想使用best_in_place gem编辑这些数据。 使用best_in_place gem时,默认情况下这是不可能的。 如何才能做到这一点?

可以通过扩展method_missingrespond_to_missing?来完成respond_to_missing? 将请求转发到序列化数据。 让我们说你有data的序列化Hash 。 在包含序列化数据的类中,您可以使用以下代码:

 def method_missing(method_name, *arguments, &block) # forewards the arguments to the correct methods if method_name.to_s =~ /data_(.+)\=/ key = method_name.to_s.match(/data_(.+)=/)[1] self.send('data_setter=', key, arguments.first) elsif method_name.to_s =~ /data_(.+)/ key = method_name.to_s.match(/data_(.+)/)[1] self.send('data_getter', column_number) else super end end def respond_to_missing?(method_name, include_private = false) # prevents giving UndefinedMethod error method_name.to_s.start_with?('data_') || super end def data_getter(key) self.data[key.to_i] if self.data.kind_of?(Array) self.data[key.to_sym] if self.data.kind_of?(Hash) end def data_setter(key, value) self.data[key.to_i] = value if self.data.kind_of?(Array) self.data[key.to_sym] = value if self.data.kind_of?(Hash) value # the method returns value because best_in_place sets the returned value as text end 

现在,您可以使用getter object.data_name访问object.data [:name],并使用setter object.data_name =“test”设置值。 但是为了使用best_in_place工作,您需要动态地将其添加到attr_accessible列表。 要执行此操作,您需要更改mass_assignment_authorizer的行为,并使对象响应mass_assignment_authorizer并使用应允许编辑的方法名称数组,如下所示:

 def accessable_methods # returns a list of all the methods that are responded dynamicly self.data.keys.map{|x| "data_#{x.to_s}".to_sym } end private def mass_assignment_authorizer(user) # adds the list to the accessible list. super + self.accessable_methods end 

所以在View中你现在可以打电话了

  best_in_place @object, :data_name 

编辑@ object.data [:name]的序列化数据

//您也可以使用元素索引而不是属性名称为数组执行此操作:

 <% @object.data.count.times do |index| %> <%= best_in_place @object, "data_#{index}".to_sym %> <% end %> 

您不需要更改其余代码。