简体   繁体   中英

Rails3.1 engine: can't get SLIM or HAML to work in test/dummy app

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?

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.

For Haml you have to put

gem 'haml-rails'

into your Gemfile

您可以使用标准的haml gem,但是在engine.rb您需要:

require 'haml'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM