如何停止 Rails 验证 xml?

发布于 2024-08-29 07:44:32 字数 1905 浏览 4 评论 0原文

我正在向 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: &quot;600&quot;
        background_color: white
        width: &quot;800&quot;
      type: text/smil-basic-layout
  body: !map:HashWithIndifferentAccess 
    par: !map:HashWithIndifferentAccess 
      text: !map:HashWithIndifferentAccess 
        left: &quot;33&quot;
        begin: &quot;33&quot;
        dur: &quot;33&quot;
        val: 34343434343434343aaaaaaa
        height: &quot;33&quot;
        width: &quot;33&quot;
        top: &quot;33&quot;

这是来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2024-09-05 07:44:32

如果您只需要嵌入任意文本,则应该使用 CDATA。只需确保字符串 ]]> 不会出现在 MESSAGE_WITH_XML 中。

xmlPostData = "<message>
                   <message-text><![CDATA[" + MESSAGE_WITH_XML  + "]]></message-text>
                   <name>" + subject + "</name>
                   <f1>" + toPhone + "</f1>
                   <f2>" + fromPhone + "</f2>
               </message>";

If you just need embed arbitrary text, you should use CDATA. Just make sure the string ]]> doesn't appear in the MESSAGE_WITH_XML.

xmlPostData = "<message>
                   <message-text><![CDATA[" + MESSAGE_WITH_XML  + "]]></message-text>
                   <name>" + subject + "</name>
                   <f1>" + toPhone + "</f1>
                   <f2>" + fromPhone + "</f2>
               </message>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文