Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 277
Rails beginner Avatar asked Feb 23 '12 21:02

Rails beginner


1 Answers

You can't force_encoding on a frozen string since that would modify it. What you can do is work with a copy:

d.map{ |d| d.dup.force_encoding("UTF-8") }
like image 188
tadman Avatar answered Sep 22 '22 23:09

tadman