Joe
Joe

Reputation: 379

How to properly pass a JSON in rails

I am new to web development and rails as well. I created a web app for internal use in php and now am converting it to rails. Trying to find out what render does is difficult. For example I find definitions like this:

render(options = nil, extra_options = {}, &block) protected

Renders the content that will be returned to the browser as the response body. 

It seems nobody told the author that you do not use a word in its definition.

I was trying to understand render because according to How to pass json response back to client that is a better way of doing the task than the approach I have tried. But without the other peices I do not know how to implement it.

Could be due to my lack of web experience so if anyone has any links to definitions thats may help please post them.

I get this error:

Error in GetData: JSON.parse: expected property name or '}' at line 2 column 3 of the JSON data

When I print the string in an alert box it appears as one long string so I do not know where "line 2" is. If I set the limit to 1 I get the same error which really makes "line 2" difficult to find.

Here is an example of the data I get back:

[{"DocumentNbr":"SS9230","DocumentRevision":""},{"DocumentNbr":"SS8640","DocumentRevision":"17"},{"DocumentNbr":"SS8618","DocumentRevision":"4"},{"DocumentNbr":"SS8630","DocumentRevision":"20"},       

I don't know if the " is supposed to be spelled out as &quot or at least thats how it is displayed in the alert box. I do not know if thats normal or an error that is causing the JSON.parse to fail. Any other ways to check data besides the alert?

I have a javascript function to call 'GetData' in the view:

var wholeNumberData;
wholeNumberData = GetData('wholeNumber', wholeNumber);

Which looks like this (stripped down version):

function GetData(getType, param) {

    var data;
    var params;

    params = 'wholeNumber=' + param;
    data = SendRequest('wholenumber/index', params);
    return data;
}

function SendRequest(source, params) {

var http = new XMLHttpRequest();

http.open("GET", source + '?' + params, false);
http.setRequestHeader("Content-type","application/json");
http.onload = function() {
        //alert('in function');
}
http.send(params);

alert(http.responseText);//Works
return JSON.parse(http.responseText);//FAILS

}

The route wholenumber/index points to an index.html.erb cionatining this:

<% @list = Wholenumber.where("DocumentNbr LIKE ?", params[:wholeNumber] + "%").limit(10) %>
<%= @list.to_json(:only => [:DocumentNbr, :DocumentRevision]) %>

Upvotes: 1

Views: 149

Answers (1)

Shaun
Shaun

Reputation: 2052

This is kind of an unusual way to do it, but you could just add html_safe to your to_json method and it should work how you have it.

<%= @list.to_json(:only => [:DocumentNbr, :DocumentRevision]).html_safe %>

If you want the control to render JSON, rather than trying to parse JSON from html, you can have the controller action do something like this:

def action
  @list = Wholenumber.where("DocumentNbr LIKE ?", params[:wholeNumber] + "%").limit(10)
  render json: @list.to_json(:only => [:DocumentNbr, :DocumentRevision])
end

The Rails Guides have a more thorough walkthrough of Rails rendering

Upvotes: 1

Related Questions