如何停止 Rails 验证 xml?
我正在向 Rails Web 服务提交以下消息:
xmlPostData = "<message>
<message-text>" + MESSAGE_WITH_XML + "</message-text>
<name>" + subject + "</name>
<f1>" + toPhone + "</f1>
<f2>" + fromPhone + "</f2>
</message>";
问题是包含带有 XML 数据的文本的字段,这是一种解决方法,但我需要能够将该 xml 提交到数据库并从那里获取它。
我可以停止 Rails 验证并替换 json 格式的 xml 吗? 这就是它的样子:
--- !map:HashWithIndifferentAccess
smil: !map:HashWithIndifferentAccess
head: !map:HashWithIndifferentAccess
layout: !map:HashWithIndifferentAccess
root_layout: !map:HashWithIndifferentAccess
height: "600"
background_color: white
width: "800"
type: text/smil-basic-layout
body: !map:HashWithIndifferentAccess
par: !map:HashWithIndifferentAccess
text: !map:HashWithIndifferentAccess
left: "33"
begin: "33"
dur: "33"
val: 34343434343434343aaaaaaa
height: "33"
width: "33"
top: "33"
这是来自 Rails Web 服务的 ruby 方法:
# POST /messages
# POST /messages.xml
def create
@message = Message.new(params[:message])
respond_to do |format|
if @message.save
flash[:notice] = 'Message was successfully created.'
format.html { redirect_to(@message) }
format.xml { render :xml => @message, :status => :created, :location => @message }
else
format.html { render :action => "new" }
format.xml { render :xml => @message.errors, :status => :unprocessable_entity }
end
end
end
是一种解决方法,但目前这必须起作用......
I'm submitting to a rails webservice the following message:
xmlPostData = "<message>
<message-text>" + MESSAGE_WITH_XML + "</message-text>
<name>" + subject + "</name>
<f1>" + toPhone + "</f1>
<f2>" + fromPhone + "</f2>
</message>";
The problem is the the field with contain a text with XML data, is a workaround but I need to be able to submit that xml to the db and get it from there.
Can I stop rails validating and replacing my xml in json format?
this is how it looks:
--- !map:HashWithIndifferentAccess
smil: !map:HashWithIndifferentAccess
head: !map:HashWithIndifferentAccess
layout: !map:HashWithIndifferentAccess
root_layout: !map:HashWithIndifferentAccess
height: "600"
background_color: white
width: "800"
type: text/smil-basic-layout
body: !map:HashWithIndifferentAccess
par: !map:HashWithIndifferentAccess
text: !map:HashWithIndifferentAccess
left: "33"
begin: "33"
dur: "33"
val: 34343434343434343aaaaaaa
height: "33"
width: "33"
top: "33"
And this is the ruby method from the rails webservice:
# POST /messages
# POST /messages.xml
def create
@message = Message.new(params[:message])
respond_to do |format|
if @message.save
flash[:notice] = 'Message was successfully created.'
format.html { redirect_to(@message) }
format.xml { render :xml => @message, :status => :created, :location => @message }
else
format.html { render :action => "new" }
format.xml { render :xml => @message.errors, :status => :unprocessable_entity }
end
end
end
Is a workaround but for the moment this has to work ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只需要嵌入任意文本,则应该使用 CDATA。只需确保字符串
]]>
不会出现在 MESSAGE_WITH_XML 中。If you just need embed arbitrary text, you should use CDATA. Just make sure the string
]]>
doesn't appear in the MESSAGE_WITH_XML.