format json in rails3 - ruby-on-rails-3

I'm trying to access a JSON version of my variable. I tried using . notation on the shows variable as I presumed it was now formatted as a json object. #shows.title but that caused an error.
I tried to use #shows.to_json.title for it to work but still no good. I thought when you format.json it already calls the to_json and that was the idea of using format or am in incorrect in thinking this way. If so what does format do then.
class ShowsController < ApplicationController
# GET /shows
# GET /shows.json
def index
#shows = Show.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #shows }
end
end
index.html.erb
<p> #shows.title <p>
the structure I'm looking for in JSON would be like this.
[
{
"id": "feature",
"width":570,
"title": "Lorem ipsum dolor amet emipsum do omnis iste natus",
"description": "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantiu mdoloremque laudantium.",
"img": "img/work/img1.jpg",
"url": "http://www.bla.com"
},
{
"id": "show",
"width":200,
"title": "Lorem ipsum dolo",
"description": "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantiu mdoloremque laudantium.",
"img": "img/work/img2.jpg",
"url": "http://www.bla.com"
}
]
UPDATED: maybe I'm not explaining this correctly.
My goal here is to have my action in the controller call the index.html page, which it does now. On that page is a jquery plugin that requests a JSON object with all the data from the database. This way it can parse through the JSON and render all the objects on the index.html page using jquery plugin. The reason for this is it's using a plugin called jquery.masonry.js & jquery.infinitescroll.js which is now setup to use multiple JSON structures to create my pages.

If you need your collection to be accessible to jquery, then you can render it as a json string inside of a script block. So for example in your index.html.erb you could put:
<%= javascript_tag "var shows = #{#shows.to_json}" %>
Which will render
<script type="type/javascript"> var shows = { "your":"json", "object":"YAY" } </script>
But you still want to respond_to html

Related

Custom 'rendering' of Display Value completing a Survey in SurveyJS

I can't find anything in the documentation of SurveyJS. If there is something I missed, a link would be great!
We have implemented a Custom Widget as described here and it works well. What we want to do next is to change the Display Value on the 'Survey Results' section when testing the survey from the creator. In other words, a 'Signature' question's result is displayed as data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAADICAYAAABS39xVAAAUGklEQVR4nO3dz28j53kH8PwZFYM6btOGAnIwWpRCe2iLAFSK2IgLUD20PTRa9GL4IiUHF2hBnZxDUuoSyT8623rLxAmpbFdCxaVn15n1luQOC3K7IW1yzRKt6HpEgQfyMOSBGPDy9CC9s8MhJc4MZzjvkN8PwIN3sdrZxerr933meZ/3KwQAEBBf8fsBAACsQmABQGAgsAAgMBBYABAYCCwACAwEFgAEBgILAAIDgQUAgYHAAoDAQGABQGAgsABM1JFGtX6Xav0upZUGHZ1ffm63qhOfYu+Cir0LqvW7fj/2SkBgwcpSRxqJnTPab5bp1tMsbebSFMocUihzSGFRoIiUpJh8rH+2y9mx/47mUhSRkhQWBf3XrYsCbRVPaLci0e1WlWr9LinDgd9...
but we don't want to do this, we want to render the result... but not for the signature but for our custom widget.
Also, is there a function to review your answers before submitting? If there is, we'll most probably also need to display a rendered answer of our custom widget here.
Showing a preview of answers before submitting
There is a SurveyJS feature, which does that. To enable it you need to add a survey-level parameter called showPreviewBeforeComplete. You can choose from the following values:
showAllQuestions
showAnsweredQuestions
This feature will automatically render all images or signatures on the preview page.
The feature is also available through the Survey Creator's UI under the "Navigation" section of the survey settings panel.
Here's an example:
{
"pages": [
{
"name": "page1",
"elements": [
{
"type": "signaturepad",
"name": "question1"
}
]
}
],
"showPreviewBeforeComplete": "showAnsweredQuestions"
}
Rendering uploaded images
If you would like to create your own widget, which renders uploaded images or signatures, you can base it on the HTML widget. It should contain an <img src='{question1}' /> tag, where the value of the src parameter should be the base64 data string, which you retrieved from the signature pad widget.
Here's an example:
{
"pages": [
{
"name": "page1",
"elements": [
{
"type": "signaturepad",
"name": "question1"
}
]
},
{
"name": "page2",
"elements": [
{
"type": "html",
"name": "question2",
"html": "<img src='{question1}' />"
}
]
}
]
}

Browse an api from mobile running by pc's localhost

If my api is running on my pc, when I browse localhost:3000/purchases I get this json response.
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
]
Now I want to get the same response by visiting the exact same url from mobile
[ both are under same wifi network ]
What should I do?
[ my server is golang with postgres, my client is angular, but I also want to see json response from my mobile ]
You just need to get your local ip of your PC :
win + R
write "cmd"
write "ipconfig /all"
find you local ipV4 "192.168.x.x"
Now you just have to visiting : "192.168.x.x/purchases"
If you really want to use "localhost:3000/purchases" to access it by your mobile, you need to install an adroid Web server app

How to pass parameters in xmlhttp (vba)

I have a program that I would like to integrate with WooCommerce. I am running into an issue when attempting to add a new product. Specifically the category and image fields. The following code works properly: (I changed to put instead of post so I would update the same product rather than continuing to add incomplete items)
myurl = "https://url.com/wp-json/wc/v3/products?sku=" & UPC & "&name=" & title & "&regular_price=" & price &consumer_key=" & rs!woocomkey & "&consumer_secret=" & rs!woocomsecret
xmlhttp.Open "put", myurl, False
xmlhttp.setRequestHeader "accept", "application/json"
xmlhttp.send
It works but sets the category to id 174 uncategorized
when I try to add the addition parts below and mess with the formatting slightly I either get the same result or the error message Below it
myurl = "https://url.com/wp-json/wc/v3/products?sku=" & UPC & "&name=" & title & "&regular_price=" & price & "&categories=[{id=" & cat2 & "}]&images=[{src=http://url.com/pos/default.jpg}]&consumer_key=" & rs!woocomkey & "&consumer_secret=" & rs!woocomsecret
{"code":"rest_invalid_param","message":"Invalid parameter(s): categories","data": {"status":400,"params":{"categories":"categories[0] is not of type object."}}}
I believe the issue is that I don't know the proper syntax when there is a subcategory. Here is the related api document from Woocommerce. I don't know PHP, Curl or the other options they list.
curl -X POST https://example.com/wp-json/wc/v3/products \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "Premium Quality",
"type": "simple",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpisegestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
}
]
Any Help would be greatly appreciated!

SwaggerResponse attribute when using HttpStatusCode.OK, does not display in UI

I have an asp.net WebAPI REST service where I have incorporated Swashbuckle. I am using the SwaggerResponse so that I get it in the UI.
eg on top of my controller method I have
[SwaggerResponse(HttpStatusCode.OK, "Success", typeof(MyModel))]
I have the following versions..
<package id="Swashbuckle" version="5.6.0" targetFramework="net47" />
<package id="Swashbuckle.Core" version="5.6.0" targetFramework="net47"
For example, if I use HttpStatusCode.Created, or any status code apart from HttpStatusCode.OK it works fine...
However, as soon as I use the HttpStatusCode.OK it just does not show up.
Why does it not appear just for the HttpStatusCode.OK?
Edit 1
The response is in the definition file created, just does not show in the UI. I.e. I have:
"responses": {
"200": {
"description": "Success",
"schema": {
"$ref": "#/definitions/ClientResultsCollectionModel[ResultsModel]"
}
},
"400": {
"description": "Invalid input data",
"schema": {
"$ref": "#/definitions/ErrorResponseModel"
}
},
"500": {
"description": "Internal Error",
"schema": {
"$ref": "#/definitions/ErrorResponseModel"
}
}
but only the 400 and 500 show in the UI.
Ok, my problem was I had css to hide the response that is displayed at the top of the document. I had this as it was often shown empty, but that was before I started adding the SwaggerResponse attributes.
It still showed up empty at the top for POSTS, but adding a [SwaggerResponseRemoveDefaults] fixed this.
So, for the GETS seems to want to put the 200 at the top, separate to the responses in the bottom section.

How to display JSON data in Dojo DataGrid using JsonRest and ObjectStore if JSON is wrapped in a Array

I am using Dojo 1.6 to render a simple DatGrid. Only problem I faced is similar to dojo 1.6 DataGrid cannot display lists?
The solution here along with reference from here http://dojotoolkit.org/documentation/tutorials/1.6/store_driven_grid/ indeed worked and all but there is still one problem with JSON structure. Following are working and non-working examples. I am getting JSON in format where everything is wrapped in items array. How do I make it work?
The working json format
[{
"bolist": ["CHM", "CVO", "PMO"],
"title": "How do I do",
"painpoints": ["this", "that", "manay more"],
"solution": "wondeful"
}, {
"bolist": ["DGM", "EXE", "CLI"],
"title": "There we go",
"painpoints": ["Front", "back", "many other places"],
"solution": "under review"
}]
The not working json format (And I am getting my json in this format)
{"items":[{
"bolist": ["CHM", "CVO", "PMO"],
"title": "How do I do",
"painpoints": ["this", "that", "manay more"],
"solution": "wondeful"
}, {
"bolist": ["DGM", "EXE", "CLI"],
"title": "There we go",
"painpoints": ["Front", "back", "many other places"],
"solution": "under review"
}]}
You can handle the json like a javascript object ! So try delivering jsonReturn.items to the grid. That way the grid gets only the json data you want:
[{
"bolist": ["CHM", "CVO", "PMO"],
"title": "How do I do",
"painpoints": ["this", "that", "manay more"],
"solution": "wondeful"
}, {
"bolist": ["DGM", "EXE", "CLI"],
"title": "There we go",
"painpoints": ["Front", "back", "many other places"],
"solution": "under review"
}]