开发者

Fill a rails form with a hashmap

开发者 https://www.devze.com 2022-12-23 12:17 出处:网络
I have a difficult situation. I let the the user create a form 开发者_如何学Cthrough a Rich Text Editor and then I save this.

I have a difficult situation.

I let the the user create a form 开发者_如何学Cthrough a Rich Text Editor and then I save this. So for example, I save this literally into my DB: http://pastebin.com/DNdeetJp (how can you post HTML here? It gets interpreted, so now I use pastebin...)

On another page I wrap this in a form_tag and it gets presented as it should be.

What I want to do is save this as a template and save the answers as a hashmap to my DB.

This works well, but the problem is I want to recreate what checkbox/radiobutton/... is selected when the user goes back to the page. So I want to fill the form with the answers from the hashmap. Is there a way to use a 'dummy' model or something else to accomplish this?

Thanks!


Since you're pasting in raw HTML which is not properly configured as a template, it is more difficult to enable the proper options based on whatever might be stored in your DB.

The reliable approach to making this work is to use Hpricot or Nokogiri to manipulate the bit of HTML you have and substitute values accordingly. This isn't too hard so long as you can define the elements in that form using a proper selector. For example, create a div with a unique id and operate on all input elements within it, comparing the name attribute with your properties. There may even be a library for this somewhere.

The second approach is to use JavaScript to enable the options in much the same fashion. This seems like a bit of a hack since the form itself will not have a proper default state.

0

精彩评论

暂无评论...
验证码 换一张
取 消