Rails3.1 engine: can't get SLIM or HAML to wor

2019-08-03 06:47发布

I'm developing a Rails 3.1 engine, and to integration test it I want to use SLIM instead of plain ol' ERB. So I tried to simply add s.add_development_dependency "slim" to my .gemspec file, but when renaming my index.html.erb file to index.html.slim, Rails complains:

Missing template dummy/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder]}. Searched in: * "/Users/josh/Documents/Work/Sientia/iq_menu/full/spec/dummy/app/views" * "/Users/josh/Documents/Work/Sientia/iq_menu/full/app/views"

I tried it also with the slim-rails gem and also with the haml-rails gem, but there renaming the file to index.html.haml resulted in the same error.

What am I doing wrong?

3条回答
何必那么认真
2楼-- · 2019-08-03 07:07

Obviously, this is an old problem, but I ran into the same thing today (this time on Rails 4), and I think I can clarify the problem, here.

Bundler serves two roles -- one is to fetch the gems and make their code available, the other is to actually "require" that code into your project.

When you add a dependency into your gemspec, it does the first function, but not the second.

In production use of your application, the dependencies identified by your gemspec are effectively added to the application's bundle, so the application's bundler will both fetch and require your gems.

If you only have the reference in the gemspec, and not in your Gemfile, then effectively nothing is doing the require, so the gem doesn't get initialized and the template engine isn't made available to your application. Adding it to the Gemfile makes it get initialized and registered.

I think you need both, for something like slim/haml. Just having the Gemfile reference means the application won't know about the dependency, and just having the gemspec reference means the engine doesn't get initialized in your dummy app.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-08-03 07:29

You can use the standard haml gem, but in your engine.rb you need to have:

require 'haml'

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-08-03 07:30

For Haml you have to put

gem 'haml-rails'

into your Gemfile

查看更多
登录 后发表回答