Marshalling data

Hello list,

For my website, I'm building a module-based customizable system, so users can pick their own modules and display them on their profile page. I want to make this as componentized as possible, but the following has me scratching my head.
My module table has an options TEXT field, in which I'd like to store marshalled options data for that specific module. I've set up everything so I can make custom templates for each module, and use the data provided in the options field. What I can't get to work is the following. An example. One of the modules is a Last.fm browse recently played tracks module. For that, I need to store the last.fm username of the user in the db. So, options => { :username => "foo" }. I'd like to be able to use it directly in my view, using something like <%= text_field 'options', 'username' %>. I seem to lack some understanding of the way Marshal.load and .dump creates data structures, so I really can't get this to work. I've tried things like:

@options = { :username => "test" }
str = Marshal.dump(@options)
@options = Marshal.load(str)

But that doesn't work. Can anybody give me some hints?

Thanks a bunch!

Bart Zonneveld wrote:

Hello list,

For my website, I'm building a module-based customizable system, so users can pick their own modules and display them on their profile page. I want to make this as componentized as possible, but the following has me scratching my head.
My module table has an options TEXT field, in which I'd like to store marshalled options data for that specific module. I've set up everything so I can make custom templates for each module, and use the data provided in the options field. What I can't get to work is the following. An example. One of the modules is a Last.fm browse recently played tracks module. For that, I need to store the last.fm username of the user in the db. So, options => { :username => "foo" }. I'd like to be able to use it directly in my view, using something like <%= text_field 'options', 'username' %>. I seem to lack some understanding of the way Marshal.load and .dump creates data structures, so I really can't get this to work. I've tried things like:

@options = { :username => "test" }
str = Marshal.dump(@options)
@options = Marshal.load(str)

But that doesn't work. Can anybody give me some hints?

Thanks a bunch!

Works for me:

[281]script/console
Loading development environment.

>> @options = { :username => "test" }
=> {:username=>"test"}
>> str = Marshal.dump(@options)
=> "\004\b{\006:\rusername\"\ttest"
>> @options = Marshal.load(str)
=> {:username=>"test"}
>> @options[:username]
=> "test"

What happens when you try it in script/console or irb?