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

老子叫甜甜 提交于 2019-12-11 03:28:44

问题


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?


回答1:


For Haml you have to put

gem 'haml-rails'

into your Gemfile




回答2:


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:


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

require 'haml'



来源:https://stackoverflow.com/questions/12508805/rails3-1-engine-cant-get-slim-or-haml-to-work-in-test-dummy-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!