使用“ember-rails”将路径从Rails迁移到Ember与现有Rails应用程序 [英] Migrate Routes from Rails to Ember with Existing Rails Application using "ember-rails"

查看:93
本文介绍了使用“ember-rails”将路径从Rails迁移到Ember与现有Rails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gemember-rails作为现有的rails应用程序。我正在尝试使用Ember路由一个资源,我已经被一些人告诉了这段代码应该可以工作,但不是这样。

Using gem "ember-rails" for an existing rails app. I'm attempting to route one resource using Ember, and I've been told by a number of people that this code should work, but it's not.

我想突破学习曲线,使其成功,但需要一些帮助。

I want to breakthrough the learning curve and make this work, but I need some help.

错误

Routing Error
No route matches [GET] "/newslinks"

代码

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

<强> 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')
});

routes.rb

routes.rb

namespace :api do
  namespace :v1 do
    resources :newslinks
  end
end

application.handlebars

application.handlebars

    <!DOCTYPE html>
<html>
<head>
<meta name="description" content="Ember - Latest" />
<meta charset=utf-8 />
<title>Ember Latest</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js"></script>
  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
  <script src="http://builds.emberjs.com/ember-latest.js"></script>
  <script src="http://builds.emberjs.com/ember-data-latest.js"></script>
</head>
<body>
  <script type="text/x-handlebars">
    <h2>Here I am</h2>
  </script>
</body>
</html>

结论

请让我知道如何建议只设置一条路线:/ newslinks通过Ember并使用现有的轨道路线进行渲染。

Please let me know how you would recommend setting up just one route: /newslinks through Ember and getting it to render using an existing rails route.

这个不是关于数据(只是想使用ember渲染路由)。此外,这段代码在jsbin中工作,所以它也不是让Ember独立于rails工作

我需要直接导轨来渲染Ember路由在routes.rb?或者Ember路由位于铁路路线的顶部,并且选择它识别的路由?

Do I need to direct rails to render a Ember route in routes.rb? Or is Ember routing sitting on top of rails routing and cherry picking the routes it recognizes?

推荐答案

如果您正在使用ember数据使用休息适配器,配置如下:

If you are using the ember data with rest adapter the configuration is the following:

给定此url your-host / api / v1 / newslinks with以下json结构:

Given this url your-host/api/v1/newslinks with the following json structure:

{
  newslinks: [
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar' }
  ]
}

您只需要映射新闻链路由:

You just need to map the newslinks routing:

App.Router.map(function() {
    this.resource('newslinks', { path: '/' });
});

并将命名空间映射到 DS.RestAdapter

And map the namespace in DS.RestAdapter:

DS.RESTAdapter.reopen({
  namespace: 'api/v1'
});

这里是使用静态适配器并嘲笑响应的现场演示。

Here is a live demo using rest adapter and mocking the response.

默认情况下,rails将服务于没有json根路径的json:

By default rails will serve the json without the root path of the json:

[
  {id: 1, name: 'foo'},
  {id: 2, name: 'bar' }
]

为了轻松实现这项工作,只需将:json 添加到 render 方法中,然后再输入数据。因此,rails将使用活动模型序列化程序,根路径将出现:

To get this work easily, in a rails controller just add the :json to your render method, followed by the data. So rails will use the active model serializers, and the root path will be present:

def index
  @users = User.all
  render json: @users
end

我希望它有帮助。

这篇关于使用“ember-rails”将路径从Rails迁移到Ember与现有Rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆