Rails 从实例变量中删除属性
在我的控制器中,我有:
def edit
@konkurrencer = Konkurrencer.find(params[:id])
@konkurrencer.attributes.map{|d| d.map{|d| d.dup.force_encoding("UTF-8") } }
end
我想删除@konkurrencer的created_at和updated_at属性,因为我无法在时间格式上调用force_encoding。
In my controller I have:
def edit
@konkurrencer = Konkurrencer.find(params[:id])
@konkurrencer.attributes.map{|d| d.map{|d| d.dup.force_encoding("UTF-8") } }
end
I want to remove created_at and updated_at attributes for @konkurrencer because I cannot call force_encoding on a time format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想要这个而不是删除这些属性:
这还将跳过您拥有的不响应force_encoding的任何其他属性。不过,这会导致一大串零,所以我认为你正在做的事情不在这里......
You probably want this instead of removing those attributes:
That will also skip any other attributes you have that don't respond to force_encoding. This results in a big string of nils, though, so I think something you're doing isn't right here...
您可以测试属性的类型,并且仅当它是字符串时才调用force_encoding。
尝试更改类似于的
部分
You can test the type of the attribute and call force_encoding only if it is a String.
Try to change the segment that is like
with