简体   繁体   English

如何在emberjs应用程序中使用Rails助手

[英]How do I use rails helper in an emberjs application

I want to use rails helper method, for example I want to use form_for or root_url or devise's sign_in? 我想使用rails helper方法,例如我想使用form_forroot_url还是sign_in?sign_in? , how could I do that? ,我该怎么做?

Using Rails helpers inside your Ember application isn't really something that is generally feasible. 通常,在您的Ember应用程序中使用Rails帮助器实际上并不可行。 You can get creative and do it by dynamically generating JS server-side, but it's not something I would consider best practice. 您可以通过动态生成JS服务器端来发挥创造力并做到这一点,但这不是我认为的最佳实践。

I could imagine it being useful to export some set of your Rails routes as JS that the application could use. 我可以想象将某些Rails路由集导出为应用程序可以使用的JS很有用。

The short answer is, you can't, at least not "out of the box". 简短的答案是,您不能,至少不能“开箱即用”。

The longer answer is... any kind of tight coupling like this between Rails and Ember would ultimately be bad for both frameworks if it was included by default.. you'd be forced to use Ember with Rails, and vice-versa. 更长的答案是……如果默认情况下包括了Rails和Ember之间的任何形式的紧密耦合,最终都会不利于这两个框架..您将不得不将Ember与Rails一起使用,反之亦然。 As it stands, Ember is pretty agnostic about your server backend, as long as it returns a proper JSON response Ember doesn't care if it's Ruby (Rails or Sinatra typically), PHP, Node.js or even static .json files. 就目前而言,Ember对于您的服务器后端是完全不可知的,只要它返回正确的JSON响应,Ember便不在乎它是Ruby(通常是Rails还是Sinatra),PHP,Node.js甚至是静态.json文件。 That gives you a lot of flexibility when building your app, but it also makes it impossible to assume things like the Rails router (or the Rails form_for helper) should exist. 这在构建应用程序时为您提供了很大的灵活性,但同时也使得无法假设应该存在Rails路由器(或Rails form_for helper)之类的东西。

If you want something like a form_for helper, your best bet would be to either write it as a Handlebars helper or (better option in my opinion) a custom View class and a handful of Handlebars helpers to give you most of what the Rails route helpers give you. 如果您希望使用诸如form_for助手之类的东西,最好的选择是将其编写为Handlebars助手,或者(我认为更好的选择)是自定义View类和少数Handlebars助手,以便为您提供大部分Rails路由助手给你。

For the route helpers, you'll want to find an automated way to export your actual Rails routes to Javascript, then go from there. 对于路线助手,您将需要找到一种自动方法,将实际的Rails路线导出到Javascript,然后从那里开始。 For a good starting point, checkout this question 作为一个很好的起点,请查看此问题

Accessing rails routes in javascript 在JavaScript中访问Rails路线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM