syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Rails, how to read a HAML file into an HTML-code-element?

Recently I encountered a use-case which asked me to read a HAML file into an HTML-code-element.

After a bit of thought I decided it was similar to this question:

In Ruby, how to read a file into a String object?

I knew this because it is easy to render any Ruby string with ERB syntax.

I typed that question into Google:

http://www.google.com/search?q=In+Ruby,+how+to+read+a+file+into+a+String+object

I found this syntax example:
mystr = IO.binread("/my/path/to/file")
I put it to use in a Rails partial like this:
%hr/
%h2 Challenge 10
- cc1018 = "#{Rails.root}/app/views/code_challenge2015_1018"
Your challenge: understand what this HAML does:
%pre
  %code.haml
    = IO.binread("#{cc1018}/demo10.haml")
The above HAML is deployed at this URL:
%br/
%a(href='https://dojo650.herokuapp.com/app/demo10.html' target='x')
  https://dojo650.herokuapp.com/app/demo10.html
%br/
%h2 Challenge 10 Questions:
%ul
  %li Which version of D3 is used?
  %li Where does the Rickshaw JS come from?
  %li What kind of element does the chart reside in?
  %li How to tell Rickshaw where to place the chart?
  %li How to change size of chart?
  %li How to change color of chart?
  %li How is area chart different than line chart?
The result can be seen at this URL:

code_challenge2015_1018

The next step in this idea is to ask IO.binread() to read from a git-repository rather than a folder.

This enhancement would allow my Rails app to display the most recent HAML file rather than an artifact from the past.


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me