I'm developing a Rails3 engine application, and I want to use Haml for the views.
First, what I have done was to add this to the engine Gemfile:
gem "haml"
While I was testing my engine, it was working OK (I have used https://github.com/josevalim/enginex to generate the gem and test it with the dummy application).
My problems started when I tried to use the engine on a real Rails application. The application does not have gem "haml" on it's own Gemfile, and so it was not initializing Haml, so I was receiving template not found errors as it was not looking for the .haml views. I was thinking that by requiring Haml on the Engine it would be enought for it to be also required by the Rails application.
What I have done for now was to add a config/initializers/haml.rb on the engine with this code:
require 'haml'
Haml.init_rails(binding)
It's working now, but I'm wondering if this is really a good way to do it.
Why Rails is not calling Haml "init.rb" file and so initializing Haml correctly by just adding gem "haml" to the engine Gemfile?
Two things are necessary. First, in the .gemspec:
s.add_dependency 'haml', ['>= 3.0.0']
And in your lib/gem_name.rb:
require 'haml'
And then run bundle both inside the gem and app directories.
I think you will have to put haml in the engine gemspec as a dependency in order for bundler to install haml in the target application (and show up in its Gemfile.lock). Something like this:
Gem::Specification.new do |s|
s.add_dependency(%q<haml>, [">= 0"])
end
I just tested this out on one of my engines. Without the dependency in the .gemspec it did not install haml in the target app (did not appear in Gemfile.lock). After I added haml to the gemspec as a dependency, it does show up:
PATH
remote: /rails_plugins/mine/my_engine
specs:
my_engine (0.0.0)
formtastic
haml
inherited_resources
settingslogic
sqlite3-ruby
GEM
remote: http://rubygems.org/
specs:
#................
haml (3.0.25)
#................
If you are using jeweler, it will add the dependencies to the gemspec automatically based on what is in your Gemfile.. it even adds a developement_dependency if you have the group defined in your Gemfile. I have only looked at enginex briefly, so I don't know if it has a similar rake task to build the gemspec.
This might help clarify some things:
http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/
Related
I have installed haml gem in my rails app (v4.2.0 beta4)
but it doesn't seem to compile any output.
I have included gem in my gemfile and ran bundle install and made sure
that I changed my html.erb files to html.haml
This is one of the template and all I see is the heading "New recipe".
%h1 New recipe
= render 'form'
= link_to 'Back', recipes_path
And nothing gets displayed for form partial.
I've come across someone with similar issue and the solution to the issue was making controller inherit
applicationController rather than actionController.
I wonder if this is what I need to do to make it working.
I would have thought installing haml gem would have handled everything needed to get it working.
In your Gemfile, put
gem "haml-rails"
And then of course bundle install
This is the ONLY gem you need to integrate HAML into your Rails app, as it provides the wrappers needed to be able to use ruby logic in your haml views. Simply using haml alone is not sufficient as haml is framework independent.
Since you've added the HAML gem (gem 'haml', '~> 4.0.5'), ran bundle install and restarted your server (Have you restarted your server??) I believe you have installed the Gem correctly.
When you say in your template file you see "New recipe", it makes me think the issue is not with the haml gem but with partial file.
Just to confirm, your partial is saved within the same folder (app/views/recipes/) as:
_form.html.haml
You need to make sure you have the underscore in front of the file name for partials.
I ended up downgrading my project to 4.1.8 and Haml works just fine.
gem 'haml-rails' was not necessary.
gem 'haml' did the job just fine.
I recently stumbled across a very cool dialect of coffeescript called contracts.coffee. I want to use it in my rails project, but I'm unsure how I can modify the compilation options of coffee-rails. In order to compile the contracts I need to run:
coffee -c --contracts script.coffee
Is the best option here to build my own gem? Or is there someway to customize the compilation parameters in the coffee-rails gem?
There's a gem for that. So just add this to your Gemfile:
gem "contracts.coffee-source", "~> 0.3.1"
I'm trying to create a gem which represents a JS and CSS library and can be included in rails projects (currently using 3.2). All the style sheets are written in SASS and depend on the compass library.
I tried adding compass-rails to the Gemfile of the "external" gem and including it in the gems SASS files using
#import "compass"
However, back in the rails application (which has a dependency to this gem), this results in an error message:
File to import not found or unreadable: compass.
Am I doing something wrong?
Update: It seems to work if I add gem compass-rails to the Gemfile of the rails application. Any change to work around that?
Thanks a lot for your help!
Besides adding compass-rails to the Gemfile of the gem, you also need to require 'compass-rails' somewhere, adding it to lib/your_gem_name.rb inside the gem worked for me.
I've come across a few tutorials where the author declares two versions of the same gem in the gemfile, even in the same group.
"haml" and "haml-rails"
"rspec" and "rspec-rails"
"cucumber" and "cucumber-rails"
and there are more examples of this....
Why is this done? Is this a better way to work with these gems rather than declaring a single gem?
Thanks
Well, these are not the same gems. Rails versions usually extend the standard libraries.
But because foo-rails has foo in its dependencies (see example here), you just need foo-rails in your Gemfile (Bundler is just great).
cucumber-rails is not the same gem as Cucumber. It's got Cucumber as a dependency, as well as some Rails-specific stuff. So as apneadiving said, if you include cucumber-rails in your Gemfile, it should load Cucumber too.
I noticed today that haml & sass have split in their upgrade to 3.1.
I used to get them both in my Rails project with gem 'haml-rails' (though, perhaps I manually added SASS to my gem directory?!? Anyway...)
I'm trying to understand dependencies & whatnot and wondering what I need to do now to get both haml & sass updated to 3.1 in my project(s)...
I see haml docs now say to use gem 'haml' to get haml...does this mean haml-rails is unnecessary/redundant now?
Based on my test, you still need haml-rails if you want .haml views automatically generated when you run rails g controller or rails g scaffold
I think it is, yes. I've just started using SASS in my Rails 3 project, and my Gemfile just contains:
gem 'haml'
... and everything appears to be working fine, as far as I can see.
UPDATE: Just realised I was actually running 3.0.25 when I wrote this post, but gem 'haml' already worked at that stage. I've just upgraded to 3.1.1, and it's still working fine :)