I am working on a ruby API's.This API fetching db value and converted into a JSON format.
This is the table data
Below code used to fetch data from db and converted into JSON format
def get_data
response= data.select(:xx, :xxx, :xxx, :xxx ).where(:id => xxx1,:xx1 => xx1)
if response.empty?
respond_to do |format|
msg = {:message => "No records found"}
format.json { render :json => msg }
end
else
respond_to do |format|
format.json { render json: response.to_json }
end
end
end
Now am getting the response like below
How can i remove the slash from the response?
Thanks in advance.
Note: I was tested this API's from POSTMAN
Executed following solutions but the result was same
use as_json instead of to_json
removed .to_json
I don't think you can use the variable response in a controller action.
It is already used by Rails: https://guides.rubyonrails.org/action_controller_overview.html#the-response-object
Related
So i'm tyring ot create a custom method in my controller
So far it looks like this
def url
require 'json'
url = url appears here
doc = Nokogiri::HTML(open(url))
doc.css(".ticket-price .h4").each do |t|
json = t.text
respond_to do |format|
format.json { render :json => {:seatname => json}}
end
end
end
However when i run this i get this error
AbstractController::DoubleRenderError
I can't see where the its being called twice? my guess however is that because its inside the each statement. What would be the best way to display the two seat names that come back in json so that it looks like this:
"seatname": "seat1",
"seatname": "seat2"
Thanks
Sam
This should work:
def url
require 'json'
url = url appears here
doc = Nokogiri::HTML(open(url))
// Constructing an array of hash
response_hash = doc.css(".ticket-price .h4").map{ |t| {seatname: t.text} }
respond_to do |format|
format.json { render :json => response_hash }
end
end
Like Max mentioned, in your code respond_to gets called for every matching element. Instead you can construct an array of hash, and then render that.
Give this a shot:
def url
require 'json'
url = url appears here
doc = Nokogiri::HTML(open(url))
response_hash = doc.css(".ticket-price .h4").map { |t| ["seatname", t.text] }.to_h
respond_to do |format|
format.json { render :json => response_hash }
end
end
In your current code, the respond_to block is being called once for each matching element in the return value of doc.css(...), which is causing the DoubleRenderError. To fix that, you need to separate constructing your response data from actually sending it. Does that help?
I'm building a Rails API and finding myself falling into the same patter:
def some_generic_customer_method
#customer = Customer.where(id: params[:id]).first
if #customer.present?
##execute some code
else
respond_to do |format|
msg = {:status => "FAILED", :messages => "Could not find customer"}
format.json { render :json => msg }
end
end
end
Is there a way to code clean my API so I'm don't have this check/failed response every API call? Right now this is being called in every one of my methods
Use before_filter and move fail-check to a private method.
I want to render a response in JSON and then, check the JSON document generated by Rails from the browser. I can't do it because a TemplateNotFound exception is raised. It's weird to me because, as it's seen, JSON is sample text and shouldn't need a template.
def who_bought
#product = Product.find(params[:id])
#lastest_order = #product.orders.order(:updated).last
if stale?(#lastest_order)
respond_to do |format|
format.atom
format.xml { render :xml => #product.to_xml(:include => :orders)}
format.json { render json: #product }
end
end
end
Anyone knows what is missing?
To get a JSON response, request type must be JSON. Append .json to the url. For e.g. localhost:3000/posts.json would return JSON data.
Ruby on Rails Question:
Inside the controller you have seven REST actions. Almost all of them have respond to do format xml/html or json. I have no idea what this means. Can you please explain it's purpose.
For example:
def index
#tweets = Tweet.all
respond_to do |format|
format.html
format.json { render json: #tweets }
end
end
What is the purpose of the "respond to" part that contains the html and json? What do these formats do?
Also, what is the difference between xml and html? Sometimes I see xml and other times html.
Thank you
Its just a way of telling your controller how to respond to different request types. For example your client could want html or xml information from you:
def index
#people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => #people.to_xml }
end
end
What that says is, "if the client wants HTML in response to this action, just respond as we would have before, but if the client wants XML, return them the list of people in XML format." (Rails determines the desired response format from the HTTP Accept header submitted by the client.)
http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to
Am working in rails. If I send a request like http://www.example.com/titles/search?search_text=something
as a respond I need a Json .How to implement this in Rails?
If you want a response in JSON format then you need to pass your request as url.json format.
In your controller, inside that action check your request format and return the result according to the requested format. You can read more about this here and having example source code as wel. Check it out here Rendering JSON and other formats.
respond_to do |format|
format.html {}
format.json {
render :json => #your_obj.to_json, :callback => params[:callback], :content_type => "text/html" }
format.xml { render :xml => response }
end
In your titles contoller in particular action, having a search_text as get param use
render json: #object
detailed: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render (2.2.8)