in Railscasts, Uncategorized

Railscast 208 ERB Blocks in Rails 3+

GitHub Repo

Heroku App

I was originally going to create the episode 40 blocks in view, but there was a comment mentioning that episode 208 was the updated version. I did watch episode 40 nonetheless, and I noticed how much Rails has improved. The way to create a block in view in Rails 2 was by concatenating and using a block.binding. This seems like a hack compared today’s Rails standard.

For comparison the code for episode 40

def admin_area(&block)
  concat content_tag(:div, capture(&block), :class => 'admin'), block.binding if admin?

Episode 208

def admin_area(&block)
  content_tag(:div, :class => "admin", &block) if admin?

The line for the episode 208 was shorter because it eliminated concat capture() and block.binding. concat was rendered unnecessary after ERB used the <%= %> tag to signify whether or not the output would be rendered to the view. capture() tried to capture the output of a block’s yield. The content_tag now is able to accept a block as an argument without the need for capture(). block.binding binds to the erb templating. I’m not sure why this became unnecessary, but I suspect the revision of erb fixed this.

Assets Precompile

While pushing to Heroku the CSS I wrote for the admin_area was not being rendered. I eventually found the answer on StackOverflow. I did not have the rails_12factor gem. After further reading of the README for rails_12factor, rails_12factor allows static assets to be retrieved despite using a proxy like Nginx. A proxy like Nginx routes the asset path of assets/rails.png to public/assets/rails.png. Rails 4 is sort of encouraging the use of a CDN to host static assets. The Readme also linked to the ’12factor’ methodology which seems to touch on devops.