字符串到序列化数组? (编号2)
我试图添加一个 before 过滤器来操作用户输入:
class FormField < ActiveRecord::Base
attr_accessible :field_type, :name, :field_options
serialize :field_options, Array
before_validation :update_field_options
def update_field_options
begin
self.field_options = self.field_options.split(/\,/).map(&:strip)
rescue ActiveRecord::SerializationTypeMismatch
errors.add_to_base("Die Optionen bitte mit Kommata trennen)")
false
end
end
end
当我尝试使用“1,2,3”创建记录时,它不起作用(ActiveRecord::SerializationTypeMismatch)。
迁移是:
t.string :field_options, :null => true
不确定我在这里做错了什么。如果我将之前的过滤器更改为:
self.field_options = [1,2,3]
它有效,似乎我无法访问 self.field_options,但我可以设置它们......
logger.warn { "field_options #{ self.field_options }" }
不输出任何内容,甚至不输出“field_options”,self 给我:
类似到:
Ruby on Rails 3 - 序列化数组不匹配的类障碍
日志:
Started POST "/global/form_fields?format=" for 127.0.0.1 at Sun Mar 20 17:50:00 +0100 2011
SQL (3.8ms) describe `shift_categories_users`
SQL (3.9ms) describe `roles_users`
Processing by Global::FormFieldsController#create as
Parameters: {"form_field"=>{"name"=>"rails verion", "field_options"=>"1,2,3", "field_type"=>"select"}, "commit"=>"Create Form field", "authenticity_token"=>"nTmPr1H3Ilp6eRqLq/9Gd0JZx7wAw0lHqGlMBEq74HU=", "utf8"=>"✓"}
User Load (5.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 261 LIMIT 1
SQL (0.9ms) BEGIN
SQL (0.4ms) ROLLBACK
Account Load (1.6ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = 'expertcloud' LIMIT 1
ConfigOption Load (1.8ms) SELECT `config_options`.* FROM `config_options` WHERE `config_options`.`name` = 'headline' AND (`config_options`.account_id = 82) LIMIT 1
DEPRIATION WARNING - make sure you use it in the correct context. user.admin? use user.role?(:admin) insted
DEPRIATION WARNING - make sure you use it in the correct context. user.admin? use user.role?(:admin) insted
SQL (3.1ms) describe `roles_users`
Role Load (2.0ms) SELECT `roles`.* FROM `roles`
Role Load (2.0ms) SELECT * FROM `roles` INNER JOIN `roles_users` ON `roles`.id = `roles_users`.role_id WHERE (`roles_users`.user_id = 261 ) LIMIT 1
Rendered shared/_options.html.haml (73.3ms)
CACHE (0.0ms) SELECT `config_options`.* FROM `config_options` WHERE `config_options`.`name` = 'headline' AND (`config_options`.account_id = 82) LIMIT 1
Role Load (1.2ms) SELECT `roles`.* FROM `roles` INNER JOIN `roles_users` ON `roles`.id = `roles_users`.role_id WHERE `roles`.`name` = 'admin' AND (`roles_users`.user_id = 261 ) LIMIT 1
SQL (2.9ms) describe `shift_categories_users`
Plan Load (1.8ms) SELECT `plans`.* FROM `plans` WHERE `plans`.`id` = 4 LIMIT 1
Rendered shared/_navigation.html.haml (179.3ms)
Rendered shared/_header.html.haml (269.9ms)
Rendered shared/_footer.html.haml (1.1ms)
Rendered global/form_fields/new.html.haml within layouts/application (1141.4ms)
Completed 200 OK in 1354ms (Views: 1231.8ms | ActiveRecord: 33.0ms)
编辑 在控制台中:
FormField.create!(:field_options => "1,2,3")
如果我尝试在模型中调试它:
puts "SPLIT #{ self.field_options }"
puts "SPLIT #{ self.inspect }"
在读取 self.field_options (即“1,2,3”)期间出现错误
i'm trying to add an before filter to manipulate user input:
class FormField < ActiveRecord::Base
attr_accessible :field_type, :name, :field_options
serialize :field_options, Array
before_validation :update_field_options
def update_field_options
begin
self.field_options = self.field_options.split(/\,/).map(&:strip)
rescue ActiveRecord::SerializationTypeMismatch
errors.add_to_base("Die Optionen bitte mit Kommata trennen)")
false
end
end
end
when i try to create a record with "1,2,3" it does not work (ActiveRecord::SerializationTypeMismatch).
migration is:
t.string :field_options, :null => true
not sure what i do wrong here. if i change the before filter to:
self.field_options = [1,2,3]
it works, it seems that i can't access the self.field_options, but i can set them.....
logger.warn { "field_options #{ self.field_options }" }
does not output anything, not even "field_options", self gives me:
similar to:
Ruby on Rails 3 - Serialize Array Mismatched Class Bafflement
LOG:
Started POST "/global/form_fields?format=" for 127.0.0.1 at Sun Mar 20 17:50:00 +0100 2011
SQL (3.8ms) describe `shift_categories_users`
SQL (3.9ms) describe `roles_users`
Processing by Global::FormFieldsController#create as
Parameters: {"form_field"=>{"name"=>"rails verion", "field_options"=>"1,2,3", "field_type"=>"select"}, "commit"=>"Create Form field", "authenticity_token"=>"nTmPr1H3Ilp6eRqLq/9Gd0JZx7wAw0lHqGlMBEq74HU=", "utf8"=>"✓"}
User Load (5.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 261 LIMIT 1
SQL (0.9ms) BEGIN
SQL (0.4ms) ROLLBACK
Account Load (1.6ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = 'expertcloud' LIMIT 1
ConfigOption Load (1.8ms) SELECT `config_options`.* FROM `config_options` WHERE `config_options`.`name` = 'headline' AND (`config_options`.account_id = 82) LIMIT 1
DEPRIATION WARNING - make sure you use it in the correct context. user.admin? use user.role?(:admin) insted
DEPRIATION WARNING - make sure you use it in the correct context. user.admin? use user.role?(:admin) insted
SQL (3.1ms) describe `roles_users`
Role Load (2.0ms) SELECT `roles`.* FROM `roles`
Role Load (2.0ms) SELECT * FROM `roles` INNER JOIN `roles_users` ON `roles`.id = `roles_users`.role_id WHERE (`roles_users`.user_id = 261 ) LIMIT 1
Rendered shared/_options.html.haml (73.3ms)
CACHE (0.0ms) SELECT `config_options`.* FROM `config_options` WHERE `config_options`.`name` = 'headline' AND (`config_options`.account_id = 82) LIMIT 1
Role Load (1.2ms) SELECT `roles`.* FROM `roles` INNER JOIN `roles_users` ON `roles`.id = `roles_users`.role_id WHERE `roles`.`name` = 'admin' AND (`roles_users`.user_id = 261 ) LIMIT 1
SQL (2.9ms) describe `shift_categories_users`
Plan Load (1.8ms) SELECT `plans`.* FROM `plans` WHERE `plans`.`id` = 4 LIMIT 1
Rendered shared/_navigation.html.haml (179.3ms)
Rendered shared/_header.html.haml (269.9ms)
Rendered shared/_footer.html.haml (1.1ms)
Rendered global/form_fields/new.html.haml within layouts/application (1141.4ms)
Completed 200 OK in 1354ms (Views: 1231.8ms | ActiveRecord: 33.0ms)
EDIT
in the console:
FormField.create!(:field_options => "1,2,3")
and if i try to debug it in the model with:
puts "SPLIT #{ self.field_options }"
puts "SPLIT #{ self.inspect }"
the error appears during reading the self.field_options (which is "1,2,3")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯..只是尝试一下,也许你的 field_options 应该是二进制类型而不是字符串类型
Hmmm.. Just taking a stab at this, maybe your field_options should be of type binary instead of string
您声明 field_options 必须从数组继承。
当您将数组分配给 field_options 时,它会为您序列化数据(在 yaml 中,但这对您来说应该是透明的。)
您不能:
因为字符串不是数组。如果您必须预处理用户输入的字符串,那么您
也可以,
然后,rails 会将您的字符串序列化为 yaml 字符串,而 self.field_options 会将其反序列化为 ruby 字符串,从而允许您拆分和映射它,并存储结果数组。
You are declaring that field_options must inherit from an array.
When you assign an array to field_options, it serializes the data for you (in yaml, but this should be transparent to you.)
you can't:
because strings aren't arrays. If you must pre-process a user-input string, you'll need to
You could also,
Then, rails would serialize your string as a yaml string, and self.field_options would unserialize it to a ruby string, allowing you to split and map it, and store the resulting array.