tricknotes / ember-cli-rails

Unify your EmberCLI and Rails Workflows
http://thoughtbot.github.io/ember-cli-rails/
MIT License
713 stars 205 forks source link

Possible to mount application to multiple routes #457

Open jgadbois opened 8 years ago

jgadbois commented 8 years ago

Which operating system and version is the project developed on? OSX El Capitan

Which version of [ruby][ruby] is the project developed on? Ruby 2.2

Which version of [npm][npm] is the project developed on? 3.3.9 Which version of [ember-cli][ember-cli] is the project developed on? Upgrading from 0.3.5 -> 0.5.8

What is the [rails][rails] version? 4.2.6

What is the [ember-cli-rails][gem] version (from Gemfile)? None

What is the [ember-cli-rails-addon][addon] version (from package.json)? 0.5.8

Is your application server [multi-threaded][](such as puma and unicorn) or is it multi-process (such as thin and webrick)? unicorn

I'm upgrading this gem and was wondering if it's possible to mount a single application into multiple routes?

Currently I have a couple different routes that load the ember application -

get "v2/(*ember_path)", to: 'exercisecom/dashboards#logger_creator', as: :ember
get "workout-plans", to: 'exercisecom/dashboards#finders', as: :workout_plans

Can I mount the same app to multiple controllers using the newer mount sytnax?

seanpdoyle commented 8 years ago

@jgadbois I'm not sure what technical limitations exist.

Have you tried?

If so, what sorts of errors are you seeing?

jgadbois commented 8 years ago

I didn't try yet, but didn't see any reference in documentation - I will report back when I get a chance to try it.

jgadbois commented 8 years ago

It does appear it's possible, but something's not working:

#config/routes:
mount_ember_app :v2, to: "exercises", controller: 'exercisecom/dashboards', action: 'finders', as: :exercises
# view - outputs the script tags in layout
<% content_for(:js_bundle) { %>
  <%= javascript_include_tag( "weighttraining") %> 
  <%= include_ember_script_tags(:v2) %>
<% } %>

Two things happening:

  1. It's outputting my rails vendor file from vendor/assets/javascription/vendor.js.
  2. I'm getting a 404 on http://localhost:3000/assets/v2.js
#config/initializers/ember.js
EmberCLI.configure do |c|
  c.app :v2, {
    exclude_ember_deps: "jquery" # including via rails assets
  }
end
#v2/config/environment.js
/* jshint node: true */

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'v2',
    environment: environment,
    baseURL: '/',
    locationType: 'history',
  .....
seanpdoyle commented 8 years ago

@jgadbois now that I understand your intent a little better, I do think there would be a problem.

I think the Ember app's dependency on a singe <base> tag value, or a single rootURL value could cause issues when making requests to both /v2/* and /workout-plans/*.

As an alternative, and a simplification, have you considered changing your Rails route to redirect traffic to a single route?

Assuming that routes prefixed with /v2/* are more desired than /workout-plans, you could try:

get "workout-plans/:rest" => redirect("/v2/%{rest}")
get "v2/(*ember_path)", to: 'exercisecom/dashboards#logger_creator', as: :ember
jgadbois commented 8 years ago

We need to have a bunch of our routes at the root level, but not everything on the root level is Ember, if that makes sense. We could migrate all our ember routes to root as long as non-Ember routes can also be there as well.

On Fri, Jun 17, 2016 at 10:45 AM, Sean Doyle notifications@github.com wrote:

@jgadbois https://github.com/jgadbois now that I understand your intent a little better, I do think there would be a problem.

I think the Ember app's dependency on a singe tag value, or a single rootURL value could cause issues when making requests to both /v2/ and /workout-plans/.

As an alternative, and a simplification, have you considered changing your Rails route to redirect traffic to a single route?

Assuming that routes prefixed with /v2/* are more desired than /workout-plans, you could try:

get "workout-plans/:rest" => redirect("/v2/%{rest}") get "v2/(*ember_path)", to: 'exercisecom/dashboards#logger_creator', as: :ember

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thoughtbot/ember-cli-rails/issues/457#issuecomment-226788720, or mute the thread https://github.com/notifications/unsubscribe/AAQYV1XUwM59l4qcpdyPnkam-oUnYfD9ks5qMrMegaJpZM4Iq68Q .

seanpdoyle commented 8 years ago

@jgadbois could you elaborate on that a bit?

Is there anything preventing that?

jgadbois commented 8 years ago

I'll be exploring this more in the next couple days - I've just upgraded my app to Ember CLI (and Ember) 2.5 and am getting EmberCLI Rails require ember-cli NPM package version to be ~> 1.13 to work properly (you have 2.5.0).

So I'm guessing I have to update to the new way of doing things now. Hopefully I can figure something out.

seanpdoyle commented 8 years ago

EmberCLI Rails require ember-cli NPM package version to be ~> 1.13 to work properly (you have 2.5.0).

There shouldn't be anything preventing you from using the 2.x.x series of ember-cli.

@jgadbois when are you seeing this?

jgadbois commented 8 years ago

I'm still on ember-cli-rails 0.3.5 to avoid the upgrade problems I've been having, but that version produces the above error.

seanpdoyle commented 8 years ago

Closing this due to inactivity.

@jgadbois if you're still having issues, I'll gladly reopen.

jgadbois commented 7 years ago

Hi Sean, Just wanted to revisit this as we're in the midst of upgrading Rails. 0.3.5 is still working but would like to catch up at some point on ember-cli-rails. Here are some more details on our situation. We mix and match ember and Rails rendering at the root level. So for instance /exercises and /dashboard/* is Ember, but /exercises/bench-press is Rails.

In 0.3.5 we didn't have to mount the app in the router, so we could map multiple routes to the same controller and load the ember assets at that point where Ember routing would take over. So the Ember root URL is / and everything just works. However, the newer way gives Ember control over everything under the mounted routes, so we'd have to add add an extra layer into our ember URLs which we don't really want.

Does that make sense?

nruth commented 7 years ago

I don't think that sounds like a good fit for how ember / ember-cli are intended to be used these days? It's more about setting up an entire app/site that has full control over the URL from / or some other rootURL.

Having an overlap sounds painful to work with: you can get rails to send people to a rails page instead of the ember page by putting that route first (#529), but when they're already inside the ember app won't it just throw an error if they try to go to one of the URLs you've only defined in the rails app? I guess you could force a page reload or something but it's not really how it's intended to be used.

You might have more success with ember-rails if you can figure out how to get the newer versions of ember to work there without ember-cli, or perhaps look at something like vue or react-rails (or glimmer?) to sprinkle some components into your rails templates.

I'd say it's out-of-scope for this gem, which might sound strange given that you are already using a previous version of it, but I imagine that previous version is from before Sean / thoughtbot adopted the gem and gave it a re-think & made it work.

Just my opinion though, perhaps there's a way I don't know about!

jgadbois commented 7 years ago

Everything works great (still using the old version of the gem with ember-cli 2.14), just would be nice to stay up to date.

More and more of the site is ember, but there's a lot of legacy pages that won't be getting updated anytime soon, so the hybrid approach is what works best for us at this point.

If Glimmer/Vue etc were options at the time we prob would have gone that route, but at this point we have a nice workflow with ember-cli.

You can close this and I'll just stay on the old version if supporting this isn't an option with the direction you've taking them gem.

seanpdoyle commented 7 years ago

@jgadbois I'm sorry, I'm still having some trouble understanding your use case, and understanding the ways the gem isn't addressing your needs.

Could you fork seanpdoyle/ember-cli-rails-heroku-example and modify it to reproduce your use case?

It would be easier for me to understand and debug your issues with concrete code examples.