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)
Related
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
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.
Here is a Rails code:
respond_to do |format|
format.html
format.xml { render :xml => #users }
format.json { render :json => #users }
end
I know what it does. But I don't know the meaning of the commands syntax-wise.
format.xml -- what is xml, is this a method which an object format has, correct? Where do I find its signature (or description)?
{ } -- a block or a hash? I think this is a block.
render -- a method? where do I find its signature (where in api docs)?
:xml => #users -- a hash, where :xml => is a key, correct?
So it could be reprented as, right?:
respond_to do |format|
format.html
format.xml do
render(:xml => #users)
end
format.json do
render(:json => #users)
end
end
format.*
Using the debugger, I learnt that format is an instance of ActionController::MimeResponds::Collector. It includes AbstractController::Collector, which uses method_missing to respond to various format calls.
if Mime::SET.include?(mime_constant)
AbstractController::Collector.generate_method_for_mime(mime_constant)
send(symbol, &block)
else
# ...
{ render xml: ... }
Looking at method_missing, it's clear that it expects a block.
render
Yes, render is a method. Documentation.
:xml => #user
Yes, it's a hash. The key is the symbol :xml. It can also be written as
render xml: #user
render(xml: #user)
render({xml: #user})
render({:xml => #user})
format comes from the request made to the method, http://app.com/controller/method.format where format could be .html or .csv etc. The meaning of format.xml is simply to check if the user requested an xml page (http://app.com/controller/method.xml.
Block, just like you describe.
At the apidock! http://apidock.com/rails/ActionController/Base/render
Correct, render will act depending on the key that exists. In your case, as the existing key is :xml the render method will output #users formatted as XML.
I have the following controller code
respond_with(#employees) do |format|
format.json { render :json => #employees.to_json(:include => :shifts) }
end
What do i have to do if i want to filter the shifts which are included? For example by a date.
I have to be able to set the filter parameters in the controller.
Edit:
I thought about using :method but it creates another variable in the json object. It has to be called "shifts"
format.json { render :json => #employees.includes(:shifts).where("shifts.date > ?", your_date_here).to_json(:include => :shifts) }
I know that if I have a url like
mysite/posts/1
The default type returned to me is html. I can get an xml version of the resource by just doing
mysite/posts/1.xml
But how do I get a json version? Is the following supposed to work?
mysite/posts/1.json
Reason I ask is because it doesn't seem to be working. So I figured I should find out if it's "supposed" to work this way before investigating further.
You're doing it right, but if the Controller isn't setup to respond to json requests you won't get anything. You'll have a respond_to block like this:
respond_to do |format|
format.html
format.xml { render :xml => #model_var.to_xml }
format.json { render :json => #model_var.to_json } #without this line, .json requests will go unanswered by the web server.
end