0

In this when I am passing phone as string in params rails api, it is being passed in the form of "\"9650661678\"". How to use escape string or how to encode it so that only string can be entered as input. Any help will be welcomed ! Thanks in advance. clients_controller.rb

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


terminal
Started GET "/clients/myzenica?phone=%229650661678%22" for 127.0.0.1 at 2016-03-15 11:21:23 +0530
Processing by ClientsController#myzenica as */*
  Parameters: {"phone"=>"\"9650661678\"", "client"=>{}}
  Client Load (0.3ms)  SELECT `clients`.* FROM `clients` WHERE `clients`.`phone` = '\"9650661678\"'
Completed 200 OK in 2ms (Views: 0.7ms | ActiveRecord: 1.2ms)
2
  • in which way you are passing params? using curl or what? Commented Mar 15, 2016 at 6:04
  • I am a ruby on rails beginner. Does nt know much about it. I am using params[:phone] in controller to take phone from the user through api. And in postman my output is just [ ]. because phone is passing as '\"9650661678\"" Commented Mar 15, 2016 at 6:07

1 Answer 1

1

You can use CGI.

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

UPDATE:

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

NOTE: In your input field which is on postman on your side. You should enter a text without double/single qoutes. Because postman field is just like input field on html tags. It will treat it all as string.

Sign up to request clarification or add additional context in comments.

10 Comments

used it like def myzenica mobile = CGI.escape(params[:phone]) client = Client.where(:phone => mobile) render :json => client end but this did not work.
Did you added require 'cgi'?
I updated my answer above, check if you do it like that. And if don't tell me what is the response.
still it is taking params as Parameters: {"phone"=>"\"9650661678\"", "client"=>{}} :(
Ok, what you are trying to do? Do you want to receive in params like this %229650661678%22? Or like this \"9650661678\" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.