Routes and Route Routes

This is a question that I have had since I started learning Ember to see how it can work with Rails.

Rails has a route.rb file with any number of existing routes. Ember has its own separate route.

How does the browser know what to look for the Ember route when the route does not exist in the rails?

It would seem logical that the rails should redirect the browser to Ember, but I did not see anything written.

In most cases, when an existing rails application exists, the route in route.rb plays the role of turning the resource into an api and makes the data accessible through a URL.

This part was simple. I can see the data using url.json

Now I am at the stage of trying to make the browser recognize one route (from many existing on the rails) through Ember routing.

This has nothing to do with displaying data. I just want to see the rendering of the template.

It seems to me that the route is simply magically recognized by the browser (without mentioning the Ember routing in routes.rb), based on what happens behind the scenes in the Ember structure, but this is not what I experienced in reality.

I keep getting a routing error:

Started GET "/newslinks" for 127.0.0.1 at 2013-08-08 12:44:30 -0700 ActionController::RoutingError (No route matches [GET] "/newslinks"):

Here is my application.js

 //= require jquery //= require jquery-ui //= require jquery_ujs //= require jquery-fileupload/basic //= require jquery-fileupload/vendor/tmpl //= require chosen-jquery //= require bootstrap //= require bootstrap-notify //= require jquery.limit-1.2.source //= require bootstrap-switch //= require handlebars //= require ember //= require ember-data //= require_self //= require app 

Here is my app.js:

 App = Ember.Application.create({ LOG_TRANSITIONS: true, ready: function() { console.log('App ready'); } }); App.Router.map(function() { this.resource('newslinks', { path: '/' }); }); App.IndexRoute = Ember.Route.extend({ redirect: function() { this.transitionTo('newslinks'); } }); App.NewslinksRoute = Ember.Route.extend({ model: function() { return App.Newslink.find(); } }); DS.RESTAdapter.reopen({ namespace: 'api/v1' }); App.Store = DS.Store.extend({ revision: 13 }); App.Newslink = DS.Model.extend({ name: DS.attr('string') }); 

I was told that this really should work, but it is not. I’m not sure where else to turn for help at this moment, so if you have any recommendations or you have little time and want to engage in freelance work, let me know.

Edit

Adding route.rb for reference:

 namespace :api do namespace :v1 do resources :newslinks end end 
+6
source share
1 answer

How does the browser know what to look for the Ember route when the route does not exist in the rails?

This is not true. When you enter the URL in the browser, the server will request it, and then the rails should respond with some content. Therefore, in this case, when the "/ newslinks" URL is requested, you want the rails to respond to the HTML page containing your ember application.

When this page is loaded, the ember application will load, and from there ember-router will be used to process links in the context of your ember application.

Make sense?

+5
source

Source: https://habr.com/ru/post/951345/


All Articles