1
votes

I'm building a rails-backed ember.js application. My main application.hbs file has a "logout" link in there and I want to make this link use the :delete method (I'm using devise to handle authentication). What I can't decide is how to output a link from a rails route. I've got all my ember routes defined in javascript, but how can I get the delete URL into handlebars?

= link_to 'Logout', app.destroy_user_session_path, method: :delete

I'd like to pass in some configuration options (urls and the like) from Rails into my Ember.Application instatiation:

window.App = Ember.Application.create()

How can I accomplish this?

1

1 Answers

1
votes

As long as the information is safe in the public's hands, you can always pop that sort of thing into a meta tag as a JSON string. You can also just make available a global config object that can be called from whatever child scope in the app you need. For example:

<meta name="rails-config" content="<%= @rails_config %>" />

@rails_config can obviously be set from the controller. You can snag that with jQuery using JSON.parse($('meta[name="rails-config"]').attr('content')) from wherever you want to initialize and store it in your ember app.

Or, just a good old fashioned global variable:

window.GLOBAL_RAILS_SETTINGS = {
  foo: '<%= Settings.foo %>',
  bar: '<%= Settings.bar %>',
  baz: '<%= @baz %>',
};

As you can see, I've also pulled in some stuff from the settings.yml (or whatever environment) file.

Hope that helps! You can store your logout url as a string and then just use a regular anchor tag or a {{link-to}} helper in your handlebars. No need to get fancy for this use-case, it seems.

There are bound to be other ways to do this. I don't know if there really is a "right" way, other than to just caution to not expose sensitive data.