Created
September 5, 2012 02:56
-
-
Save nhocki/3629583 to your computer and use it in GitHub Desktop.
Marshal problem with Liquid Templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rails console | |
1.9.3p194 :017 > Part | |
=> Part(id: integer, name: string, liquid_source: text, template_source: text, created_at: datetime, updated_at: datetime) | |
1.9.3p194 :018 > p = Part.first | |
1.9.3p194 :019 > p.liquid_source | |
=> "Hello {{name}}" | |
1.9.3p194 :020 > p.liquid_source.encoding | |
=> #<Encoding:UTF-8> | |
1.9.3p194 :021 > p.template_source = Marshal.dump(Liquid::Template.parse(p.liquid_source)).force_encoding('UTF-8') | |
=> "\u0004\bo:\u0015Liquid::Template\u0006:\n@rooto:\u0015Liquid::Document\u0006:\u000E@nodelist[\aI\"\vHello \u0006:\u0006ETo:\u0015Liquid::Variable\b:\f@markupI\"\tname\u0006;\tT:\n@nameI\"\tname\u0006;\tT:\r@filters[\u0000" | |
# Notice the `\u0000` at the end | |
1.9.3p194 :022 > p.template | |
=> #<Liquid::Template:0x007ff7dd769288 @root=#<Liquid::Document:0x007ff7dd769210 @nodelist=["Hello ", #<Liquid::Variable:0x007ff7dd769120 @markup="name", @name="name", @filters=[]>]>> | |
1.9.3p194 :023 > p.save | |
=> true | |
1.9.3p194 :024 > p.reload.template | |
Part Load (1.0ms) SELECT "parts".* FROM "parts" WHERE "parts"."id" = $1 LIMIT 1 [["id", 1]] | |
ArgumentError: marshal data too short | |
from /Users/tesla/code/templates/app/models/part.rb:9:in `load' | |
from /Users/tesla/code/templates/app/models/part.rb:9:in `template' | |
from (irb):24 | |
from /Users/tesla/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start' | |
from /Users/tesla/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start' | |
from /Users/tesla/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>' | |
from script/rails:6:in `require' | |
from script/rails:6:in `<main>' | |
1.9.3p194 :025 > p.template_source | |
=> "\u0004\bo:\u0015Liquid::Template\u0006:\n@rooto:\u0015Liquid::Document\u0006:\u000E@nodelist[\aI\"\vHello \u0006:\u0006ETo:\u0015Liquid::Variable\b:\f@markupI\"\tname\u0006;\tT:\n@nameI\"\tname\u0006;\tT:\r@filters[" | |
# Notice the `\u0000` at the end is missing! | |
1.9.3p194 :028 > Marshal.load(p.template_source + "\u0000") | |
=> #<Liquid::Template:0x007ff7dd6b48b0 @root=#<Liquid::Document:0x007ff7dd6b4838 @nodelist=["Hello ", #<Liquid::Variable:0x007ff7dd6b4748 @markup="name", @name="name", @filters=[]>]>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Part < ActiveRecord::Base | |
def render(opts = {}) | |
template.render opts | |
end | |
def template | |
@template ||= Marshal.load(template_source.force_encoding('utf-8')) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm currently having the same problem except I can't save the marshaled template to MySQL since I get a "invalid byte sequence in UTF-8" type of error. I narrowed it down to the template field. Was wondering if you figured out how to force the utf-8 encoding upon marshaling?
Thanks,
Bill