Beginner Rails Environment in a Vagrant Box

One of the hardest parts of getting started in Rails development is setting up your computer. One of the hardest parts of teaching rails development is dealing with the all the special case problems that arise when students are running different development environments on different operating systems. One solution I’ve heard proposed from a couple different sources is to have all beginners develop in a vagrant virtual machine, that way the teacher only has to deal with the peculiarities of one environment and students can follow one simple set of instructions to set up their computers.

The problem is that poking around the Internet, I couldn’t find a beginner friendly simple set of instructions anywhere or a vagrant vm box setup specifically for Rails development beginners. This blog entry will hopefully provide Rails beginners with both simple instructions to get started and virtual machine to download. Continue reading

Using Partials to make controller specific navigation in Rails

I’ve been playing with partials lately and while I am still a Rails newbie. I stumbled upon what I think it is a pretty cool trick. I like to render my navigation in a partials to modularize my layout. I noticed that when I do do this I have to specify the layout view directory in the render call because the controllers are rending the layout  so the code has to be like this:

<%= render "layouts/nav" %>

Then I realized I could use this to render controller specific partials by placing _nav.html.erb partials in the specific view directories and adding a little logic using the params hash. So I changed my layout nav code to this:

<% if lookup_context.exists?("nav", params[:controller], true) %>
  <%= render "nav" %>
<% else %>
  <%= render "layouts/nav" %>
<% end %>

So now if the controller view directory has a _nav.html.erb it renders that and if not it defaults to the one in the layout directory. Of course the same thing could be achieved using content_for methods but I find this way is much more intuitive and less cluttered.

Addendum

Chris Saunders rightly pointed out that in MVC conditional logic should not be in the views, ideally it should be in an object like the controller itself. That wouldn’t work in this case, but it is still better to put the conditional logic in the Application Helper to get it out of the view like this:

module ApplicationHelper
  def navigation
    if lookup_context.exists?('nav', params[:controller], true)
      {partial: 'nav'}
    else
      {partial: 'layouts/nav'}
    end
  end
end

And then call it from the layout.html.erb like this:
<%= render navigation %>

Today’s Itinerary

  • Swimming {from 9:00}
  • Read Ruby on Rails 4.0 Guide  {unti 11:30}
  • Yoga {noon – 13:15}
  • Complete 2 Bloc.io Checkpoints
  • Take Alisa Swimming [4:00]
  • Darren’s for Darren’s bookclub {20:00-22:00}

Done Dashing (for now…)

I was so good for a couple days last week about updating this blog and then I disappeared off the face of the Interwebz. I was Dashing, errr, working on a widget for Shopify’s Dashing gem for Shopify’s Dashing Widget Contest.

Well I put in a lot of effort, learned a lot, had a lot of fun but I didn’t get it done in time. But I nonetheless did get done. I called my widget Ntile. It is ready to be used and downloaded.

Ntile – A widget for Shopify's Dashing gem
Ntile – A widget for Shopify’s Dashing gem

In the future I have some refactoring I want to do on the code and some features I want to add but for the time being I’m back to learning Ruby on Rails double full time.

Code or Bust

The path of the data science samurai is not always easy or clear. After 15 months of continuing unemployment I choose to cancel a job interview this morning. It was for a mindless minimum wage job. Not that there is anything wrong with mindless minimum wage jobs, a pay cheque is a pay cheque.

My decision to not take the job is bizarrely enough because I don’t feel I have the time. Even though I haven’t been employed, in the last 15 months I have been working, and for the last 4 months or so working really hard on various data science and coding projects and training.  I have yet to find my way to a pay cheque, but I love what I’m doing and I want to continue doing it.

I feel fortunate to finally have discovered work that I am passionate about. I realize that most people work at something they are not passionate about. Having found what I am passionate about I want to push my luck and continue to try to make a living at it. My current financial position is well below the poverty line, however there is still food on the table for my wife and little girl so I feel I still have luxury of  pursuing a living at what I love to do. So that is where I am… code or Bust!

Today’s Itinerary

  • Write Blog {done by 9:00}
  • Finish Luminosity Training {done by 9:30}
  • Go to Code Factory
  • Bloc.io {until noon or Week 4 completion}
  • Lunch
  • Review Long’s work on Project Night Meta {done by 14:00}
  • Dashing Contest {until 17:00}
  • UX/UI Design Meetup {at 18:30}

Increasing Bash Prompt Awesome on your Mac

Ever since I saw Jason Seifer’s custom bash prompt in a Team Treehouse Video I’ve been keen to adopt the trick myself. The feature that really attracted me was the ability to see what branch you are currently on in a Git project but also seeing your Ruby version and gem set is super useful as we transition from a Ruby ~1.9 Rails ~3.2 world to a bold new Ruby 2.0 Rails 4.0 world.

So last night I spent an hour making my prompt dreams a reality and here is the result:Bash Prompt 2013-09-24 at 8.45.14

My prompt is now  “user | Ruby version & gem set | git branch”. No one guide had all the features I wanted so here is a list of the guides I used:

  1. Taylor McGann’s Blog – Customize Your Shell & Command Prompt
  2. Martin Fitzpatrick MRES – ADD GIT BRANCH NAME TO TERMINAL PROMPT (MAC)
  3. ariejan de vroom – Ruby version and gemset in your Bash prompt? Yes sir!

What I learned was that your bash prompt is defined by a variable named $PS1. The best place to set this variable is in your .bash_profile on a mac. Here is how I changed mine:

#####################
### FUN FUNCTIONS ###
#####################

# Ruby Gemset and Version for prompt
function rvm_version {
  local gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}')
  [ "$gemset" != "" ] && gemset="*$gemset"
  local version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $2}')
  [ "$version" != "" ] && version="Rv$version"
  local full="$version$gemset"
  [ "$full" != "" ] && echo "$full "
}

# Git Branch for prompt
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

###############################
### ENVIRONMENTAL VARIABLES ###
###############################

# Prompt
export PS1="\u \[33[0;31m\]\$(rvm_version) \[33[0;93m\]\W\[33[32m\]\$(parse_git_branch)\[33[00m\] $ "

Enjoy!

Today’s Itinerary

  • Complete Blog {By 9:30}
  • Luminosity Training {By 10:00}
  • Ruby Book {Finish by 11:00}
  • Yoga {From 12:00 – 13:00}
  • Bloc.io {13:30 – 17:00}
  • Meeting with Aaron {17:00 – 17:45}
  • Ruby Tuesday {18:30 – 21:00}

Make it so.

In the past I have experimented publishing itineraries on facebook. It was fairly effective. There is something about publicly declaring what I intend to accomplish today, that motivates me to make it so. There is certainly an aspect of public humiliation if I don’t achieve what I set out to do but that feeling is mitigated by the knowledge that my blog while, posted publicly,  enjoy  privacy through obscurity and apathy. I think mostly just the mental exercise of envisioning how I want to get through my day sets the target. Like everybody I have many wants and desires competing for attention and just taking a few moments to sort them out in the morning makes it much easier to accomplish things.