[Help] Fetching from a rails api


#1

Hey!

I am using the ember-rails gem. I made a route in my rails application called /status which returns a bunch of json. I then created a route in ember called status (/#/status). I was wondering how I can create a ember controller that gets the json from /status and displays it in the template?

Sorry, im a total newb and I have no idea how to make the api call.

Thanks for your time.


#2

Checkout ember-data or google jquery ajax.


#3

Hey!

I made this file:

Counterstrike.StatusRoute = Ember.Route.extend(model: (params) ->
  return Ember.$.getJSON config.api.baseURL + '/status', (response)
)

but its giving me this error:

No idea how I can fix it.


#4

Are you using ember data?

My ember data routes typically look like this

export default Ember.Route.extend({
  model: function() {
    return this.store.find('status');
  }
});

When I use Ember.$.ajax, which is probably more analogous to your getJSON, my url is just api/status. I think if you set the baseURL you don’t need to reference it because it assumes it’s the base of the URL. I would try just removing the config.api.baseURL part and see what happens.


#5

I figured it out. config wasnt set…

Im stuck on a different issue now.

I have this code: https://gist.github.com/nahtnam/425fc6cf7f8c762a1efc

If I try running that handlebars file, it doenst work. It just turns up empty (no errors in the console).

Any ideas what is happening? I verified that ember is receiving the json correctly. If I do something like {{model.result.matchmaking.search_seconds_avg}}, it returns 62…


#6

Hi,
You are iterating inside the datacenter subfield but not rendering anything.
Also, the JSON it’s not so good for me. I’ll transform it like this:

        "datacenters": {
            {
                "Name": "EU West",
                "capacity": "full",
                "load": "medium"
            },
            {
                "Name": "EU East",
                "capacity": "full",
                "load": "medium"
            },

And then in your status.js.handlebars I’ll use this:

{{#each model.result.datacenters as |datacenter|}}
  <div class="panel">
    <div class="panel-heading">
      <div class="row">
        <div class="col-sm-10 text-left">
          {{datacenter.name}}
        </div>
        <div class="col-sm-2 text-right">
        <div class="label">{{datacenter.capacity}}</div>
        </div>
      </div>
    </div>
  </div>
{{/each}}

Something like that. Hope it helps


#7

Hey!

Thats not working either. I replaced everything with asdf to see if it atleast iterates, but it doesnt. It just comes up blank.

Thanks. (No errors in console).


#8

Hi,
if your app is public and you can link the repo maybe we can test it.
For personal experience, I can say that ember-inspector is a great tool for debugging.
In your case, I’ll see if the model is loaded or not.


#9

Hey!

Here is the code. https://github.com/nahtnam/csgo

Its a rails application so you would have to install ruby, and rails, and then cd into the folder and run bundle install. Then you would have to create a pg database called counterstrike_development and run rake db:migrate. Finally run rails server and it should work.

Thanks.

EDIT: The ember app is in /app/assets/javascripts/