How to install and use Slim template engine with Middleman - ruby

I'm new to Middleman and ruby in general.
I've installed Ruby
I've installed Middleman and the gems to get it running.
I need to use slim instead of the default template system.
So I installed the Slim gem. Slim's website only says that I need to require 'slim' in order to get it to work.
The middleman website says I only need add the template engine to the config.rb file, but it gives no examples...
For someone with no ruby background, this is no help.
I looked for several config.rb on git and they all have:
require 'slim'
And
# Set slim-lang output style
Slim::Engine.set_default_options :pretty => true
# Set template languages
set :slim, :layout_engine => :slim
I added that to my config.rb file and created the layout.slim and the index.html.slim
When I refresh my local server I get:
Not Found
`/' not found.
I have middleman installed with Boilerplace. I'm not sure if there are more files that I need to change, but I can't find any good resources online, which is odd.
Could anyone give me some direction as to what I'm missing?

So here we go... after much reading and searching google for examples I think I figured it out.
To get Slim working with Middleman
Add gem "slim" to your project's gemfile
go to command line, in your project folder and gem install bundler
In the config.rb file add require 'slim'
Start the middleman server to test it

The middleman-slim project by yterajima is helpful in this regard.
Install is very easy.
$ gem install middleman
$ gem install middleman-slim
$ middleman init PROJECT_NAME --template slim

Bundler tip: you can also include multiple gems at once using Bundler.require. If you have gems in groups, you can include them as such: Bundler.require :group1, :group2 ...

Related

How to include Ruby Gems on Github

So, I've created a GitHub to manage my latest Ruby project, and I want for it to utilize a couple of gems. On my PC, all I have to go is type
gem install "gemName"
and it loads it to my computer, and then all I have to do in my Ruby script is have
require "rubygems"
require "gemName"
How can I do this with GitHub? What I tried to do is create a subfolder from the main repository (called "RubyGems") and then in my main ruby script
require "/RubyGems/colorize"
require "/Rubygems/psych"
With the two gems (colorize and psych) in the "RubyGems" folder.
Is this the proper way to do this? Will this even work? What is the right way to do this? (Sorry, I'm kinda new to GitHub.)
A couple of things, unless you're using a really old version of Ruby (like 1.9) you don't need to require 'rubygems' because is already required by default, next I highly recommend you to get familiar with bundler.
Bundler is used for "bundling" the required gems you use, to so do you have to install the gem (gem install bundler) and then you create a Gemfile, like this:
source 'https://rubygems.org'
ruby '2.2.0'
gem 'colorize', git: 'https://github.com/fazibear/colorize.git'
gem 'psych'
Execute bundle install after, that will create Gemfile.lock file, make sure you push both files to your repository.
With that you would be able to bundle exec ./your-script.rb, assuming your script is something like this:
require 'psych'
require 'colorize'
# Here I do stuff with psych and colorize

Ruby script cannot load a gem installed via bundler

I am trying to include the ruby-mysql gem in my ruby script. I have installed the gem using bundler but when I run bundle exec ./mysql_connector, I receive the error ./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError). Can you please help me troubleshoot what the problem is?
What I did
Installed rails in my home directory.
I do not have root access to the server so I have installed rails in my local directory by following the instructions here:
http://www.r-bloggers.com/installing-ruby-on-linux-as-a-user-other-than-root/
Created a directory for my application.
My application resides in my home directory in a folder called connector. It has a Gemfile that looks like this:
source 'https://rubygems.org'
gem 'ruby-mysql'
Call bundle install.
Using ruby-mysql 2.9.14
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Add dependencies to my script. My script is in connector/mysql_connector and it reads:
#!/home/dcox/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'ruby-mysql'
Make script executable. I saw that you need to run bundle exec using an executable file, so I followed the instructions here to make my script executable: http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
Run the script. I execute using bundle exec mysql_connector and see:
/home/dcox/bin/mysql_connector:4:in `require': cannot load such file -- ruby-mysql (LoadError)
from /home/dcox/bin/mysql_connector:4:in `<main>'
Is it the $LOAD_PATH? After searching around for answers, I discovered a lot of SO answers as well as a blog post (https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-load-such-file/) that seem to suggest the problem is that the gem is not installed in a directory on the $LOAD_PATH. Here is what I see when I run $LOAD_PATH from IRB:
irb(main):002:0> $LOAD_PATH
=> ["/home/dcox/lib/ruby/site_ruby/2.1.0",
"/home/dcox/lib/ruby/site_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/site_ruby", "/home/dcox/lib/ruby/vendor_ruby/2.1.0",
"/home/dcox/lib/ruby/vendor_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/vendor_ruby", "/home/dcox/lib/ruby/2.1.0",
"/home/dcox/lib/ruby/2.1.0/x86_64-linux"]
Next I checked to see the location of ruby-mysql:
dcox#analytics1:~/connector$ bundle show ruby-mysql
/data/home/dcox/connector/vendor/bundle/ruby/2.1.0/gems/ruby-mysql-2.9.14
Obviously my connector/vendor/bundle path is not on the $LOAD_PATH. I could add it, but I have a feeling I am missing something simple here because bundler is supposed to work as long as you follow the instructions, right?
Any advice or help is very much appreciated! Thanks!!
If you just want to require this specific gem, require 'mysql' should work (e.g., https://github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10).
Your file should call Bundler.setup http://bundler.io/bundler_setup.html
Better yet, if you instead call Bundler.require(:default) it will setup and require all the gems in your Gemfile for you.

Way to add gem file to Dashing framework

I need to use Rally API dev kit rally_rest_api gem. I'm new with ruby. Is the any way to do it quickly ?
Thanks for helping !
You should have a file called Gemfile in the root of your Dashing project. It should already contain the dashing gem itself:
gem 'dashing'
Simply add the following below it:
gem 'rally_rest_api'
And run bundle install. To actually use the gem in your project, take a look at the documentation.

Middleman - Unknown Extension: livereload

Trying to just get started building a site with Middleman, and I'm following the tutorial to a t, but when I start the server I always get "Unknown Extension: livereload"
I already have
group :development do
gem 'middleman-livereload'
end
and
configure : development do
activate :livereload
end
Any ideas?
I had the exact same problem. For some reason LiveReload isn't recognized when you include it in a group in your Gemfile. My fix:
gem 'middleman-livereload' # NOTE: breaks if placed inside a group!
group :middleman do
gem 'middleman'
end
I'm using middleman 3.3.12 and middleman-livereload 3.4.2
I started with the blog flag out of the box.
middleman init MY_BLOG_PROJECT --template=blog
So, for me, the gem was never part of the original GEMFILE. My solution was to add the gem.
# Gemfile
gem 'middleman-livereload'
Ruby Static Sites FTW!

Add the gem I'm developing to be loaded automatically by rubygems without build/install?

I'm developing a gem with Jeweler in a custom directory.
I want to be able to require the gem within any app (and also the executables files from $PATH), and without needing to build and install the gem each time I modify it.
I thought about 2 ways:
I make a symlink to $GEM_HOME/gems and $GEM_HOME/bin
I add the bin directory to $PATH and the lib directory to rubygems to be loaded.
But I bet there is a proper way to do this.
You can specify a local path in the gem command:
gem 'your-gem', '1.2.3', :path => 'path/to/your-gem'
Update: As #Nick points out in the comments,
This is specific to using bundler. In general, it's just require '/path/to/your-gem.
I'd like to add, however, that if you're using a custom-developed gem, bundle will probably make your life easier if you're not already using it. This is because with bundler, when you're done developing the gem (or at a stable/release point) you can load a gem directly from a github repository like this:
gem 'your-gem', :git => 'git#github.com:you/your-gem.git'
No need to mess around with your Gemfile
require 'bundler/setup' will put the right gem into your $LOAD_PATH and allow you to require it on the next line.
#!/usr/bin/env ruby
require 'bundler/setup'
require '<gem-name>'

Resources