简体   繁体   English

Rails无法修改冻结的字符串

[英]Rails can't modify frozen string

In my controller I have: 在我的控制器中,我有:

def index
    @title = 'asdsadas'
    @kategoris = Tag.where("name like ?", "%#{params[:q]}%")
    respond_to do |format|
    format.html
    format.json { render :json => @kategoris.map(&:attributes).map{|d| d.map{|d| d.map{|d| d.force_encoding("UTF-8") } } } }
    end
end

I get this error in view: 我看到此错误:

RuntimeError in Admin::TagsController#index

can't modify frozen string

You can't force_encoding on a frozen string since that would modify it. 您不能对冻结的字符串进行force_encoding ,因为那样会对其进行修改。 What you can do is work with a copy: 您可以做的是使用副本:

d.map{ |d| d.dup.force_encoding("UTF-8") }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM