Pergunta

I want to use rails helper method, for example I want to use form_for or root_url or devise's sign_in?, how could I do that?

Foi útil?

Solução

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. 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. 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.

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.

For the route helpers, you'll want to find an automated way to export your actual Rails routes to Javascript, then go from there. For a good starting point, checkout this question

Accessing rails routes in javascript

Outras dicas

Using Rails helpers inside your Ember application isn't really something that is generally feasible. You can get creative and do it by dynamically generating JS server-side, but it's not something I would consider best practice.

I could imagine it being useful to export some set of your Rails routes as JS that the application could use.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top