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.
Related
Newbie here, I've recently implemented the webpacker gem in my react-rails project. Being new to webpack and react, this gem makes a lot of sense to me coming from a rails background.
i'm wondering if it were possible to use the react-rails gem in my rails webpacker project so we could still use the view helper method like this:
<%= react_component('MyComponent', #controller_value.to_json)%>
if not, is there a reason why?
Thanks for any input! :)
First you need to install node- I used nvm,
and install yarn- link.
Then, add these lines to your Gemfile:
gem 'webpacker', '~> 2.0'
gem 'react-rails'
then run
bundle install
rails webpacker:install
rails webpacker:install:react
rails generate react:install
This will create the necessary files in app/javascript (not app/assests/javascripts) and config files.
Now add <%= javascript_pack_tag 'application' %> to the appropriate layout file, like app/views/layouts/application.html.erb
Finally you will need to run ./bin/webpack-dev-server in a separate terminal, along with rails server.
That's it. Now you can use
<%= react_component('MyComponent', #controller_value) %>
Here MyComponent will be defined in app/javascript/components/MyComponent.js
You can use another gem for this
--> #react_on_rails (5.2.0)
$ bundle show react_on_rails
--> #- /Users/mark/.rvm/gems/ruby-2.1.2/gems/react_on_rails-5.2.0
Isn't the helper supposed to be included something like this?:
See http://pothibo.com/2013/06/customize-rails-view-controller-with-railtie/
and https://github.com/reactjs/react-rails/blob/master/lib/react/rails/railtie.rb#L42-L44
Hear is the example of gem react_on_rails and gem react-rails
https://learnetto.com/blog/rails-data-react-component-webpacker
I've been browsing stack overflow and I noticed that many people use simple_form to make their lives easier.
I wanted to give it a try, so I added the gem to my gem file (gem 'simple_form') and sent:
rails generate simple_form:install --bootstrap
After reading the installation message, I created a view with the sample code from the readme:
<%= simple_form_for #user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
Unfortunately, I get this error as soon as I try to load the page:
undefined method `simple_form_for' for #<#<Class:0x69dd688>:0x6903ac8>
What gives? How come it can't even recognize the method? I feel like I'm missing something really simply here. Could it be because i'm using bootstrap-sass? Do I need to include a helper in my user controller?
Did you restart your server?
Always keep in mind that after you install a gem or plugin.
Always restart your server
Its all because you didn't restart your server after installing new libraries such as simple_form, devise etc.
Try stop your server and restart it. Your code is perfect!. I encountered this several times. The solution is just restarting the server!.
You must restart your server after installing simple_form gem.
For any absolute beginners. This error will occur if you forgot to install simple_form. To rectify, add this to your Gemfile
gem 'simple_form'
and bundle install
To summarize all possibilities from above, hopefully this list is useful for other gems too.
Check if the simple_form gem is installed correctly
Find the gem in Gemfile and Gemfile.lock. Another way is to run this command in your terminal: bundle list | grep simple_form
If the output is empty
Possibly simple_form isn't installed in your project yet. Since the gem is an external gem, it isn't included in the Rails framework. Rails has its own form library (aka form_with and form_for).
To add simple_form to your project, open the Gemfile and add gem 'simple_form', then run the command in your terminal bundle install.
If the output isn't empty
Check if you have restart the server yet.
After restart the server, and you still not be able to access SimpleForm constant
Check if the gem is set to require: false in Gemfile. If so, remove this part and run bundle install.
You can check if the gem is loaded from rails console by typing rails console or rails c in your terminal.
After the console is loaded, type SimpleForm and hit Enter. In the case there is some errors complaining about the constant is undefined, refer to previous steps to fix.
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 :)
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/
the line
rails new someapp -m haml
doesn't work. It seems to need a path to some where.
Update: haml-rails is in fact installed by gem install haml-rails but the line above wouldn't work.
The really short version
Generate a new rails app based on a simple template that sets up Haml out of the box (and some other nice optional features).
rails new ProjectName -m https://raw.github.com/RailsApps/rails3-application-templates/master/rails3-haml-html5-template.rb
http://decielo.com/articles/377/haml-by-default-in-a-new-rails-3-2-app
Also check this out:
https://github.com/RailsApps/rails-composer
EDIT:
If you want to do this through the "gem" you simply need to run the default command
rails new myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
This is a safe command as it points to the master branch of the gem and will be a stable URL. Once you have run this command you will be prompted with options. Simply select HAML and SASS when asked by the wizard.
Gem haml-rails allows to generate views in Haml, but not the initial layout.
After running rails new someapp (note: w/o -m haml) and adding line gem "haml-rails" to your Gemfile, you just need to rename application.html.erb to application.html.haml and manually convert its content from ERB to Haml.
After that, all generated views will be in Haml.
app/views/layouts/application.html.haml
!!!
%html
%head
%title "HAML'd"
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= yield
Make sure you have the haml-rails gem installed.
Don't forget to add gem 'haml-rails' to your Gemfile.
Trivial, but make sure you restart your rails server after adding the haml gems and run bundle install. This got me the first time around.
Install the gem html2haml and you can instantly change the html content to haml from within vim. Have a look at this - http://www.economyofeffort.com/2014/07/20/convert-html-to-haml-within-vim-buffer/