在rails api中编码字符串参数

时间:2016-03-15 05:58:48

标签: ruby-on-rails ruby

当我在params rails api中将手机作为字符串传递时,它以<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <link href="/eidsar/css/headerStyle.css" rel="stylesheet"> <link href="/eidsar/css/pageStyle.css" rel="stylesheet"> <title>EIDSAR-Login Page</title> </head> <body> <div> <div id="header-top"> <a href="/eidsar" title="Logo"> <img class="logo" src="/eidsar/images/MobistarLogo.jpg" /> </a> <ul class="lang-nav"> <li><a id="active" href="?lang=en" title="en">EN</a></li> <li><a href="?lang=fr" title="fr">FR</a></li> <li><a href="?lang=nl" title="nl">NL</a></li> </ul> </div> <div id="header-bottom"> <div style="visibility:hidden" id="header-content"> <ul class="head-nav"></ul> </div> </div> </div> <c:if test="${not empty error}"> <div class="errorblock"><spring:message code="login_unsuccessful_msg" text="default text"/> ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}</div> </c:if> <div class="loginBackground"> <div class="loginFont">Log In</div> <form name='form1' action="<c:url value='j_spring_security_check' />" method='POST'> <label class="fieldStyle"><spring:message code="login.username" text="default text"/></label> <input class="fieldBox" style="margin-top: 30px; margin-left: 15px;" type='text' name='j_username' /> <br> <br> <label class="fieldStyle"><spring:message code="login.password" text="default text"/></label> <input class="fieldBox" style="margin-left: 25px;" type='password' name='j_password' /> <br> <input class="loginSubmit" type="submit" value="Log In" /> </form> </div> </body> </html> 的形式传递。如何使用转义字符串或如何对其进行编码,以便只能输入字符串作为输入。 欢迎任何帮助!提前致谢。 clients_controller.rb

"\"9650661678\""


def myzenica
    mobile = params[:phone]
    client = Client.where(:phone => mobile)
    render :json => client
end

1 个答案:

答案 0 :(得分:1)

您可以使用CGI

require 'cgi'
CGI.escape('%229650661678%22')
 => "\"9650661678\""

更新:

def myzenica
  require 'cgi'
  mobile = CGI.escape(params[:phone])
  client = Client.where(:phone => mobile) 
  render :json => client
end

注意:   在您身边postman的输入字段中。您应该输入没有双/单qoutes的文本。因为postman字段就像html标签上的输入字段一样。它会将它全部视为字符串。