Rails和骨干一起工作

问题描述:

我刚开始看MVC结构,首先我看着如何 Backbone.js的工作,现在我刚刚完成的rails 的,由code学校。我知道我没有钻研过远到任何这一点,但我有一个问题的开始。

I am just starting to look at MVC structure, first i looked at how backbone.js worked, and now I have just completed rails for zombies, by Code School. I know that I haven't delved too far into any of this, but I had a question to begin with.

您可以使用这些库一起?

我已经学会了如何创建模式的意见等在这两个,但创建一个真正的应用程序时做您同时使用骨干和Rails?

I have learned how to create models, views, etc in both, but when creating a real application do you use both backbone and rails?

如果这样...

当你使用 Backbone.js的模型与一个模式?

When do you use a backbone.js model vs. a rails model?

也许我只是自己走在前面的,需要不断练习,做教程,但我似乎无法找到任何直接在此。

Maybe I am just getting ahead of myself and need to keep practicing and doing tutorials but I couldn't seem to find anything directly on this.

谢谢!

什么我会建议采取看看thoughtbot的的 Backbone.js的on Rails的的书,这是一个很好的起点,虽然旨在中级到高级的读者。我买了有这本书已经在轨,但作为一个整体Backbone.js的初学者工作,并一直担任我非常好。

Before anything else I'd suggest taking a look at thoughtbot's Backbone.js on Rails book, which is a great starting point, although aimed at an intermediate to advanced audience. I bought this book having already worked with rails but as a total backbone.js beginner and it has served me very well.

除此之外,还有与组合这些框架从而超越涵盖在这本书和其他书籍一些细节根本问题。下面是一些事情,我建议你想想,从我自己的经验配对回报率和Backbone.js的。这是一个长期的答案,从偏离你的问题的具体一点,但我希望它可以帮助你出理解你面临的问题的大画面的感觉。

Beyond that, there are some fundamental issues with combining these frameworks which go beyond the details covered in this book and other books. Below are some things I'd suggest you think about, from my own experiences pairing RoR and backbone.js. This is a long answer and strays a bit from the specifics of your question, but I hope it might help you out in the "big picture" sense of understanding the problem you're facing.

您的Rails应用程序的顶部使用Backbone.js的时候面对的第一件事是做什么的有关意见,不过这的确是一个更深刻的问题只是表面。这个问题去意味着什么来创建一个RESTful Web服务心脏。

The first thing you confront when using backbone.js on top of a rails application is what to do about views, but this is really just the surface of a much deeper issue. The problem goes to the very heart of what it means to create a RESTful web service.

Rails的设置开箱鼓励其用户创建RESTful服务,通过构建一组以统一的URI获取资源方面的路由(在的routes.rb $定义C $ C>文件),通过标准的HTTP动作。所以,如果你有一个发表模式,您可以:

Rails is set up out of the box to encourage its users to create RESTful services, by structuring routing in terms of a set of resources accessed at uniform URIs (defined in your routes.rb file) through standard HTTP actions. So if you have a Post model, you can:


  • 通过发送获取的所有帖子 GET 要求 /岗位

  • 创建通过发送 GET 要求 /职位/新,填写表格并发送一个新的职位它( POST 请求) /岗位

  • 更新ID为后 123 通过发送 GET 要求 /职位/ 123 /编辑,填写表单和它( PUT 要求)发送到职位/ 123

  • 销毁ID为后 123 通过发送删除要求 /职位/ 123

  • Get all posts by sending GET request to /posts
  • Create a new post by sending a GET request to /posts/new, filling out the form and sending it (a POST request) to /posts
  • Update a post with id 123 by sending a GET request to /posts/123/edit, filling out the form and sending it (a PUT request) to posts/123
  • Destroy a post with id 123 by sending a DELETE request to /posts/123

要记住的Rails这方面最关键的事情是,它是从根本上的无国籍的:不管是什么,我pviously做$ P $,我可以创造一个新的发布只需通过发送一个 POST 要求使用有效的表单数据到正确的URI,说 /岗位。当然也有注意事项:我可能需要先登录(有一个会话cookie识别我),但在本质上的Rails并不真正关心我在做什么我发送的请求之前。我可以跟进,通过更新另一篇文章,或发送到任何其他资源的有效作用是提供给我的。

The key thing to remember about this aspect of Rails is that it is fundamentally stateless: regardless of what I was doing previously, I can create a new Post simply by sending a POST request with a valid form data to the correct URI, say /posts. Of course there are caveats: I may need to be logged in (have a session cookie identifying me), but in essence Rails doesn't really care what I was doing before I sent that request. I could follow it up by updating another post, or by sending a valid action to whatever other resources are made available to me.

如何Rails的设计这一方面使得它比较容易转(Javascript的光)Rails Web应用程序到一个API:资源将是类似或相同的Web框架返回的HTML页面,而API(典型值)在JSON或XML格式返回数据。

This aspect of how Rails is designed makes it relatively easy to turn a (Javascript-light) Rails web application into an API: the resources will be similar or the same, the web framework returning HTML pages while the API (typically) returns data in JSON or XML format.

骨干也是基于RESTful资源。每当您创建,更新或毁灭一个Backbone.js的模型,您通过发送到其承担上述种类的RESTful架构的URI标准的HTTP动作这样做。这使得它非常适合与RESTful服务像RoR的整合。

Backbone is also based on RESTful resources. Whenever you create, update or destroy a backbone.js model, you do so via the standard HTTP actions sent to URIs which assume a RESTful architecture of the kind described above. This makes it ideal for integrating with RESTful services like RoR.

但有一个微妙的点这里要强调:Backbone.js的使用Rails 作为一个API 无缝集成。也就是说,如果你抛开HTML视图和只使用Rails的的在职RESTful资源,与数据库整合,执行会话管理等方面,那么它集成了非常漂亮的结构骨架.js文件提供了客户端code。许多人认为,没有什么错用钢轨这样 ,我认为在很多方面他们是正确的。

But there is a subtle point to be stressed here: backbone.js integrates seamlessly with Rails as an API. That is to say, if you strip away the HTML views and just use Rails for serving RESTful resources, integrating with the database, performing session management, etc., then it integrates very nicely with the structure that backbone.js provides for client-side code. Many people argue that there's nothing wrong with using rails this way, and I think in many ways they are right.

的并发症如何处理,我们刚刚扔掉的Rails的其他部分做的问题,但出现:观点和他们重新present什么

The complications arise though from the issue of what to do with that other part of Rails that we've just thrown away: the views and what they represent.

这实际上是比它最初可能显得更加重要。 HTML视图重新present人类用来访问服务提供了RESTful资源的无状态接口。破除他们给你留下两个接入点:

This is actually more important than it may initially seem. HTML views represent the stateless interface that humans use for accessing the RESTful resources your service provides. Doing away with them leaves you with two access points:


  1. 对于人类来说:由Backbone.js的层提供一个丰富的,客户端接口(状态)

  2. 对于机器:由轨道层提供一个面向资源的RESTful API(无国籍)

注意,不再有对人类无状态(REST式)接口。相反,在与API传统的Rails应用程序,我们有一些东西更接近这样的:

Notice that there is no longer a stateless (RESTful) interface for humans. In contrast, in a traditional rails app with an API, we had something closer to this:


  1. 人类HTML资源(无国籍)

  2. 机器
  3. JS​​ON / XML资源(API)(无国籍)

用于访问资源后两个接口的的比previous两个比较接近在性质上彼此。试想例如Rails的 respond_with ,这需要相似的优势,在一个统一的方法来包装各种RESTful的反应

The latter two interfaces for accessing resources are much closer in nature to each other than the previous two. Just think for example of rails' respond_with, which takes advantage of the similarities to wrap various RESTful responders in a unified method.

这可能都显得很抽象,跑题了,我知道了。为了尽量使其更加具体,考虑下面的问题,这回来到你有关获取轨道和Backbone.js的共同努力问题。在这个问题中,你希望:

This might all seem very abstract and beside the point, I know. To try to make it more concrete, consider the following problem, which gets back to your question about getting rails and backbone.js to work together. In this problem, you want to:


  • 创建使用Backbone.js的丰富的客户端体验,护栏为JSON格式提供资源后端的Web服务。

  • 使用pushState$c$c>给每个页面中的应用程序的URL(例如 /职位/ 123 ),它可以直接访问(通过输入其在浏览器栏)。

  • 对于这些网址,也有助于为客户的HTML页面的JavaScript没有

  • Create a web service with a rich client-side experience using backbone.js, with rails as the back end serving resources in JSON format.
  • Use pushState to give each page in the app a URL (e.g. /posts/123) which can be accessed directly (by entering it into the browser bar).
  • For each of these URLs, also serve an HTML page for clients without javascript.

这些都不是一个现代化的网络服务不同寻常的要求,但他们创造一个复杂的挑战。为了使长话短说,你现在要创建的两个的以人为本层:

These are not unusual demands for a modern web service, but they create a complex challenge. To make a long story short, you now have to create two "human-oriented" layers:


  1. 状态的客户端接口(Backbone.js的模板和视图)

  2. 无国籍HTML资源(Rails的HTML视图)

在实际上这样做的复杂性导致很多时下放弃这两个的后面,只是提供了一个丰富的客户端接口。什么你决定做什么取决于你的目标,你想达到什么目的,但它是值得认真思考这个问题。

The complexity of actually doing this leads many nowadays to abandon the latter of these two and just offer a rich client-side interface. What you decide to do depends on your goals and what you want to achieve, but it's worth thinking about this problem carefully.

至于这样做的另一个可能的参考,我建议有一个看O'Reilly的 REST Web服务。这听起来很奇怪,在一个​​关于Rails和Backbone.js的问题,推荐一本书上休息,但其实我觉得这是一个适合这些非常不同的框架一起关键部分,并更充分地理解它会帮助你把优势两者的优势。

As another possible reference for doing that, I'd suggest having a look at O'Reilly's RESTful Web Services. It might seem odd to be recommending a book on REST in a question about Rails and Backbone.js, but actually I think this is the key piece that fits these very different frameworks together, and understanding it more fully will help you take advantage of the strengths of both.