The first thing you need to do is add a couple gems to the Gemfile. The slim gem tells Rails how to parse the Slim syntax. The slim-rails gem overrides the Rails generators for ERB and replaces them with Slim generators. Add these gems to your Gemfile as below:
Gemfile
12
gem'slim','~> 2.0.3'gem'slim-rails','~> 2.1.5'
Then run bundle install to install the gems:
1
bundleinstall
Then generate a Homes controller with a show action by running the command below:
1
railsgcontrollerHomesshow
Notice that the generated view ends in .slim, Rails has generated a slim file for us, courtesy of the slim-rails gem. Now let’s modify our routes file to set up a resource and add a root path. Open up your routes file (config/routes.rb) and modify it so that it looks somethings like:
routes.rb
12
rootto:"homes#show"resource:home,only:[:show]
Now you are ready to play with Slim. Open up the show view for homes (app/views/homes/show.html.slim) and modify it so that it looks somethings like:
show.html.slim
1234567891011121314151617
javascript:alert("HELLO!!!!!")h1Welcometomysite<b>inlinehtmlissupported</b>p | The time is #{Time.now}, I Like to program in Ruby on Rails.#hashtag I threw a hashtag in here!#classy I'm classy!p = Random.rand(10)- if Random.rand(6) == 1 |Lucky number 1! You win!brbrsmall Goodbye!
Which translates to:
1234567891011121314151617181920
<scripttype="text/javascript">alert("HELLO!!!!!")</script><h1>Welcome to my site</h1><b>inlinehtmlissupported</b><p>The time is 2015-02-22 00:24:59 - 0400, I Like to program in Ruby on Rails.</p><divid="hashtag">Ithrewahashtaginhere!</div><div id="classy"> I'm classy!</div><p>2</p><br><br><small>Goodbye!</small>
Slim is a replacement for ERB, similar to HAML. Slim provides a nice lean syntax that’s easy to pick up and learn. In this article we will learn how to use Slim. Let’s run through this with me.
Basic Syntax
Slim is actually really easy to learn. Indentation matters, but the indentation depth can be chosen as you like. If you want to first indent 2 spaces, then 5 spaces, it’s your choice.
Example:
12
p|Thisisaverylongparagraph
Which translates to:
123
<p>Thisisaverylongparagraph.</p>
In the above example you’ll notice two things. First, the pipe (|) character is used to delimit free-form text on a new line. Second, we simply used p to specify the paragraph tag. This works for any tag, for instance you could just as easily have done:
12
h1|LargeTitle
Or more simply:
1
h1LargeTitle
In the above example that we didn’t specify the pipe (|) character. The pipe (|) character is only needed if we are entering free-form text on a new line.
You can create a div with a default class by using:
1
class
Which translates to:
1
<divclass="class"></div>
You can create a div with an id by using:
1
#id
Which translates to:
1
<divid="id"></div>
What about html attributes? Well, we specify them similarly to how we would in html:
Well, all is great, but what about Ruby code? Inserting Ruby code is pretty easy. Code that doesn’t return a result, such as if statements or loops, are prefixed with a dash (-).
So far if you create a Rails app, you know that it can quickly to do things like:
- Add RSpec and Cucumber.
- Disable/turn off Test Unit.
- Switch the default database engine to PostgreSQL.
- Add other Gems such as HAML, CoffeeScript, …
Luckily we can customize what the Rails app generator generates. In this article, I will use Rails app templates to customize the default behavior of the Rails application generator. Let’s run through this with me.
Rails App Templates
Rails app templates allow you to run actual commands during the generation process. For example, I want to install rspec and haml, create a readme.md file, and commit the whole thing to git. I can easily do this by creating an Rails application template. Let create a file called app_template.rb and add the codes below:
If you generate a new app with the parameters -m app_template.rb it will run the instructions you listed in the template. The code listed above will get executed when you run the following command:
1
railsnewblog-mapp_template.rb
If you browse your Rails project you’ll see that rspec, haml got installed and the default readme file was removed and a readme.md file was added, and finally everything was committed to git.
In addition, you can have the application template prompt the user for more information. The code listed below allows you to do like this:
app_template.rb
1234567891011121314151617
remove_file'README.doc'create_file'README.md'gem'rspec-rails'gem'haml-rails'run'bundle install'generate'rspec:install'ifyes?'Do you wish to generate a root controller? (y/n)'name=ask('What do you want to call it?').underscoregenerate:controller,"#{name} show"route"root to: '#{name}\#show'"route"resource :#{name}, only: [:show]"endgit:initgitadd:'--all',commit:'-m "Initial Commit"'
In the code above, the yes? function will prompt the user with a yes/no question. The ask function will prompt the user for text input. In this case, we ask the user if they wish to generate a root controller, and if they say yes, we prompt them for a name.
Now you have a basic grasp on the Liquid template engine itself, let’s learn how to utilize it in our Ruby on Rails application. First you need to add the gem to the Gemfile. Add the following line to the Gemfile:
1
gem'liquid','~> 2.6.1'
Then run bundle install to install the Liquid gem:
1
bundleinstall
Then create a model called Page. The Page model will be used to represent a page owned by a specific user. Run the commands below to create this model:
Page.delete_allPage.create(id:1,user:"JingLong",template:%{ Hello {{ user }}, here is your shopping list. <ul> {% for item in list %} <li>{{ item.name }}</li> {% endfor %} </ul>})Page.create(id:2,user:"Bunlong",template:%{ What is up my man? Here is your shopping list. <ul> {% for item in list %} <li>{{ item.name }}</li> {% endfor %} </ul>})Page.create(id:3,user:"Rubyist",template:%{ HTTP 200: Shopping List Found Items in your list: <ul> {% for item in list %} <li>{{ item.name }}</li> {% endfor %} </ul>})
Then run rake db:seed command to seed the database with your sample data:
1
rakedb:seed
Then create a homes controller that will allow you to play with liquid. Run the command below to create the homes controller:
1
railsgcontrollerHomesshow
Then create a pages controller. This controller will be used to display a user’s page. Run the commands below to create the pages controller:
1
railsgcontrollerPagesshow
Then modify your routes (config/routes.rb) file so that it looks somethings like below:
Then modify your homes controller (app/controllers/homes_controller.rb) to pull down a list of all the user’s pages. Open up your homes controller and modify it so that it looks somethings like below:
Then modify your pages controller (app/controllers/pages_controller.rb) to pull down the correct page for the specified user. Open up your pages controller and modify it so that it looks somethings like below:
Then create a few helper (app/helpers/pages_helper.rb) methods for use with this example. Open up your pages helper and modify it so that it looks somethings like below:
The codes above gives you some sample data to play around with.
Then modify your pages show view (pp/views/pages/show.html.erb) to look somethings like below:
show.html.erb
12345
<h1><%= @page.user %>'s Personal Page</h1><% template =Liquid::Template.parse(@page.template)%><%= template.render(shopping_list(@page.user)).html_safe %>
The codes above tells Rails to render your template using the Liquid Template Engine.
Finally, open up the show view for your home controller (app/views/homes/show.html.erb) and add in the code listed below:
show.html.erb
123456
<h1>UserPages</h1><ul> <% @pages.each do |page| %> <li><%= link_to "#{page.user}'s page", page_path(page) %></li><% end %></ul>
If you fire up a rails server and visit your development site, you’ll notice that you can browse each user’s pages and get a customized version of each one.
However, what if you want to use the Liquid templating engine as a replacement for erb itself? take it easy, first, create a new file in the lib folder called liquid_view.rb (lib/liquid_view.rb) and add in the codes listed below:
Liquid is a templating engine that allows you to enable your users to quickly and easily customize your views at run-time while maintaining the safety, security, and integrity of your servers.
Liquid Syntax
In liquid, there are two different types of markup.
The first type of markup is output markup, is denoted by double sets of curly braces.
1
Hi{{user.name}}
The second type of markup is tag markup, is typically used for logic and control structures such as loops.
Using version control, is best to commit your code in small, discrete chunks rather than making large commits. However, What happens when you are working on a large change, and your boss comes to you and tells you they need an urgent bug fixed? With the git stash command you can quickly and easily store your code away and recall it for later use. Let’s run through this with me.
Stash the Code
1
gitstash
Reapply the Changes You Sent to the Stash Type
What if you want to reapply your changes? Use git stash apply to reapply all the changes you stashed away with git stash.
1
gitstashapply
List All of the Stashes You’ve Made Type
Git stash supports stashing multiple times. To see a list of all the code you’ve stashed, use git stash list. The git stash list will show you a list of all the stashes you’ve made.
1
gitstashlist
Stash Apply From the Earlier List
If the first stash is named stash@{1}, you can type git stash apply stash@{1} to apply the changes from that stash.
You maybe have noticed a file called secrets.yml in the config directory of a Ruby on Rails 4.1 project. This feature was added as part of Rails 4.1 in order to have a common storage location for the keys and credentials for various services. You can use the secrets.yml for everything from AWS credentials to your secret_key_base (the default in Rails 4.1).
You have add this file to your .gitignore file to avoid accidently pushing your keys to git. You can also store your production keys in this file if you wish.
To access the various keys in the secrets.yml file:
1
Rails.application.secrets.key_name
Example: The following code will returns the merchant login.
1
Rails.application.secrets.active_merchant_login# returns 112233 on development/test
ActiveModel::Dirty is a library that is built into Ruby on Rails that allows you to quickly and easily track what attributes on a model have changed. This article will show you how to use it.
Checking Whenever the Model Has Changed
Checking whether the model has changed using the changed? method:
1
product.changed?# => false
If you change an attribute on the model, the changed? method will returns true:
12
product.name="Mac Pro"product.changed?# => true
Tracking the Change
You can track what the change by using the attr_change method: