Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image assets #193

Closed
fullofcaffeine opened this issue Aug 4, 2015 · 3 comments
Closed

Image assets #193

fullofcaffeine opened this issue Aug 4, 2015 · 3 comments

Comments

@fullofcaffeine
Copy link

What's the recommended way to deal with image assets? Since Sprockets helpers like image-path - which are pretty much needed because of assets fingerprinting on production - are only available in Rails-rendered views, what's the suggested solution to be able to reference images from the Rails pipeline from hbs views (rendered by Ember)?

I can imagine a couple of hacks for doing that, but the question is: Does ember-cli-rails already implement (or recommend) something?

Thanks!

@ghost
Copy link

ghost commented Aug 8, 2015

Hey!

Here is what I currently use:

Put this in your application helper:

def image_assets
    image_asset_dir = Rails.root.join('app', 'assets', 'images')

    assets = {}

    %w(.jpg .png .jpeg .svg).each do |file_ext|
      Dir.glob(File.join(image_asset_dir, "**", "*#{file_ext}")).each do |absolute_path|
        file = absolute_path.sub(File.join(image_asset_dir, '/'), '')
        assets[file] = asset_url(file)
      end
    end

    assets
  end

Add this to you /app/assets/javascripts/assets.js.erb:

<% environment.context_class.instance_eval { include ApplicationHelper } %>

window.assets = window.assets || {};

window.assets.images = <%= raw(image_assets.to_json) %>;

Create a helper:

return window.assets.images[params[0]];

then you can do something like this in your template:

<img src="{{asset-path 'login.png'}}">

EDIT: This isnt my code, I found it somewhere online and set it up in my application

@fullofcaffeine
Copy link
Author

@nahtnam Thanks a lot for taking the time to reply, and for the code!

I eventually decided to not use ember-cli-rails. What I'm doing now is to not use the Rails assets pipeline at all. I moved everything to Ember CLI's pipeline. The pipeline is very close (if not the same, or more) in terms of feature to Sprockets, so I'm not missing anything, I think.

When developing, I run both:

  1. ember serve --proxy http://localhost:3000 --watch
  2. bundle exec rails s

When in development, Rails won't serve any index HTML. It will be served by the ember Express server. This way, I also make it simpler to play with Ember CLI's conventions and architecture, instead of fighting it and adapting to it.

Now, when deploying, to make things simpler (yeah, I know there are better ways to do that, such as storing the index HTML in Redis, etc), I just build the ember app with the help of a helper sh script, I borrowed from here:

https://github.com/knomedia/ember-cli-rails

It builds the app, lets Ember CLi generate all the assets and the index HTML, and then copies the contents of the ember-generated index.html to Rails' application.html.erb. This way, if I want to, say, deploy to Heroku, it's a piece of cake, because Rails will serve the index for me, and then Ember will take charge. Rails is essentially only used as an API backend.

Cheers!

@ghost
Copy link

ghost commented Aug 11, 2015

For me, the perfect scenario would be to have rails s boot up both the rails and ember server (like it does now), but when I deploy it, it compiles it and moves to to the public folder or to rails application.html.erb...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant