Brain Dump

Because My Tiny Brain Can Only Retain So Much

Helpful Rails Debug Snippets

Session dump:

<%= debug session %>

Params dump in a controller:

render :text => params.inspect

Single-line comments in a Rails view:

<%# code %>

Multi-line comments in a Rails view (be sure <% =begin %> and <% =end %> are not indented):

<%
=begin %>
<div class="sign-in-container">

    <h2>Sign In</h2>

    <%= form_tag sessions_path do %>

      ....some stuff

  <% end %>

</div>
<%
=end %>

Multi-line comments in Ruby (be sure =begin and =end are not indented):

=begin
    def edit
        @user = User.find params[:id]
    end
=end

Rails Routes When Singular and Plural Are the Same

Rails RESTful routes are a wonderful thing. However, if you’re building a resource that is the same singular as it is plural, such as News, you may run into a snag. Let’s first take a look at a normal resource, Users, so we have something to compare to. If we run rake routes on Users we get:

1
2
3
4
5
6
7
8
users_index GET    /users/index(.:format)    users#index
      users GET    /users(.:format)          users#index
            POST   /users(.:format)          users#create
   new_user GET    /users/new(.:format)      users#new
  edit_user GET    /users/:id/edit(.:format) users#edit
       user GET    /users/:id(.:format)      users#show
            PUT    /users/:id(.:format)      users#update
            DELETE /users/:id(.:format)      users#destroy

Notice that on line 2 users takes us to users#index while on line 6 user takes us to users#show. So if you think about it, how would this work with News? It wouldn’t.

Library Not Loaded: libmysqlclient.18.dylib

If you’ve ever gotten an error while going a ‘gem install mysql2’ like so:

dlopen(/Users/rarneson/.rvm/gems/ruby-1.9.3-p125/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/rarneson/.rvm/gems/ruby-1.9.3-p125/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/rarneson/.rvm/gems/ruby-1.9.3-p125/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

You’ll need to create a symlink for libmysqlclient.18.dylib:

$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Octopress’ Rake New_post Doesn’t Work With Zsh

Creating a post with Octopress uses a rake command like so rake new_post["My new post"]

However, when you run this within the zsh shell it doesn’t work:

$ rake new_post["My new post"]
zsh: no matches found: new_post[My new post]

If you care about why this is, visit this Github issue. If you don’t really care and you just want a work around, use rake new_post\["My new post"\]

From the discussion in the Github issue above it sounds like imathis is planning to update the new_post command in Octopress to something like this:

rake new_post
Enter a post title:

Also worth mentioning, rake set_root_dir[your/path] doesn’t work for the same reason. So you’ll need to do rake set_root_dir\[your/path\].