Putting html through xml rails 3
I'd like to update html data through an xml put request. So what I've done is make an ajax request with the data
$.ajax({
type: "PUT",
url: "/network_internet_control_layouts/1.xml",
data: '<network-internet-control-layout><layout>'+$("div#temp").html()+'</layout></network-internet-control-layout>',
contentType: 'application/xml',
dataType: 'xml',
success: function(msg) {
alert( "Data Saved: " + msg );
}
});
When I look at my console, I see that the html code is escaped
Processing by NetworkInternetControlLayoutsController#update as XML
Parameters: {"network_internet_control_layout"=>{"layout"=>{"div"=>{"class"=>"tables", "div"=>{"style"=>"position: relative;", "class"=>"table", "div"=>{"class"=>"tablePart simple", "div"=>{"class"=>"block",开发者_高级运维 "network_infrastructure_id"=>"1", "network_infrastructure_name"=>"VISI-201-NW"}}}}}}, "id"=>"1"}
And when it gets saved, it is totally wrong
AREL (0.5ms) UPDATE `network_internet_control_layouts` SET `layout` = '--- !map:ActiveSupport::HashWithIndifferentAccess \ndiv: !map:ActiveSupport::HashWithIndifferentAccess \n class: tables\n div: !map:ActiveSupport::HashWithIndifferentAccess \n style: \"position: relative;\"\n class: table\n div: !map:ActiveSupport::HashWithIndifferentAccess \n class: tablePart simple\n div: !map:ActiveSupport::HashWithIndifferentAccess \n class: block\n network_infrastructure_id: \"1\"\n network_infrastructure_name: VISI-201-NW\n', `updated_at` = '2011-05-05 11:29:33' WHERE `network_internet_control_layouts`.`id` = 1
Is there a way to insert the raw data in the db?
Thanks
Checkout this SO question where the jquery is using CDATA for the HTML inside of XML
Try :
data: '<network-internet-control-layout><layout><![CDATA[' + $("div#temp").html() + ']]</layout></network-internet-control-layout>'
From rails3, by default it html codes will not be embeded. How ever if you still want to embed html you have to use
raw
ex:
html_code = "<table><tr><td>This is a Sample</td></tr></table>
<%= raw html_code%>
will print a html table
HTH
sameera
精彩评论