Alfa Jango Blog Engineering, Software, and Entrepreneurship

Posts Tagged ‘Ruby’

Rails js.erb Remote Response not Executing

Tuesday, March 15th, 2011

A common issue that comes up in the GitHub Issues for the Rails jQuery UJS and the Remotipart gem / jQuery plugin goes like this:

The JavaScript response from my js.erb is being interpreted as text and not being executed.

I usually point to this comment in GitHub Issues, which I posted the first time I came across this problem. But GitHub Issue comments are difficult to link to, and almost useless in search results due to all their ajaxy-ness. So, I’ll repost and explain here for future reference.

(more…)

Counting Block Nesting Depth in Ruby
(an Exercise in Exception-handling, Thread-safety, & Meta-programming)

Tuesday, November 30th, 2010

This question recently arose on the SFRuby users group mailing list. Given some nested block methods, Steven Harms asked how we could track the current block-nesting depth during runtime:

method1 "something" do
  method2 "something else" do
    "hiya"                    # < = We are now 2 block levels deep
  end
end

Unfortunately, Ruby does not have any built-in runtime methods to return the current block nesting depth. So, it looks like we’ll have to roll our own.

(more…)

method_missing:
A Rubyist’s Beautiful Mistress

Friday, October 8th, 2010

I’ve read a few articles lately (like this one), advocating the use for the method_missing method in Ruby.

Many people seem to have a passionate love affair with method_missing, but aren’t very careful in how they handle their relationship. So, I’d like to address the question:

How should I use method_missing?

If you don’t want to read through the reasons you should stay with your companion, define_method, and are just looking to rationalize your love-affair with method_missing, you can skip directly to when to use method_missing.

When to resist method_missing

First of all, never give in to your love affair with method_missing without taking a moment to realize how good you have it. You see, in your day-to-day life, you rarely need method_missing as bad as you think you do.

Day-to-day life: proxy methods

Case: I need to allow one class to use the methods of another class.

This is the most common use-case I’ve seen for method_missing. It’s especially popular in gems and Rails plugins. The pattern goes something like this:

class A
  def hi
    puts "Hi from #{self.class}"
  end
end

class B
  def initialize
    @b = A.new
  end

  def method_missing(method_name, *args, &block)
    @b.send(method_name, *args, &block)
  end
end

A.new.hi #=> Hi from A
B.new.hi #=> Hi from A

(more…)

How to Include Your Rails App Layout in Your WordPress Theme (or any PHP application)

Tuesday, February 23rd, 2010

Here’s the problem: You have a Ruby on Rails application and its WordPress blog. For the sake of consistency and branding, you’d like to use the same layout in your WordPress blog as you do for your Rails app. Perhaps your blog is one of the main links or tabs in your application’s header.

Why this doesn’t work: You proceed with creating a custom theme for your WordPress blog that looks exactly like your Rails application. Then over the next couple years, you begin to realize how tedious this solution is. You have to copy over your CSS stylesheets from your Rails app, your Javascript files (if your standard layout contains any Javascript elements, like a Twitter widget in the footer or whatever), and your layout images and assets. Then, every time you change or update the layout of your Rails app, you have to copy the changes in your custom WordPress theme. And you cannot just copy the changes, because if you have any Ruby/Rails functionality in the layout, you have to translate them into PHP functionality in your WordPress layouts.

And then there’s the impossible stuff… if anything in your Rails app layout loads information from the database in your Rails app, then you cannot have those elements in your WordPress layout, unless you do something like create an XML or JSON feed in your Rails app and then import it in your WordPress layout. But this is quite tedious still if you want to change any of it!

There’s an easier way.

Include Your Rails Layout Directly In Your WordPress Layout

The solution I found turns out to be quite simple. The basic idea is to create a :partial in your Rails app layout, make it publicly accessible via your config/routes.rb, have it generate and simply return the appropriate html using your controller method, and then import that html (and CSS) in your WordPress layout.

(more…)

How to Monitor Your Rails/Passenger App with Munin

Thursday, February 11th, 2010

Munin is a great tool to monitor resources on your server, showing graphs over time, so that you can analyze resource trends to find what’s killing your server before it causes major problems. It is also very configurable and can be made to profile and graph just about anything via plugins. And with a couple tricks, you can get it to monitor your Phusion Passenger application with ease.

Update: If you have RVM installed on your server and need Munin to work with RVM’s Passenger installation, follow all of the instructions below, and then make the changes described in Monitor Passenger with Munin when using RVM.*

* These changes will still use your server’s non-RVM-installed default Ruby to run the Munin plugin, which will in turn use your RVM-installed Ruby to run the passenger-status and passenger-memory-stats commands.

If you already have Munin installed and working, and just want to know how to get the Passenger Plugins working, you can skip directly to Install Munin Passenger Plugins, Configure Munin for Passenger Stats, and be sure to read the Gotcha.

(more…)

Cool Libraries for Ruby

Monday, December 28th, 2009

I was just thinking about how often I come across cool projects and libraries people are developing for Ruby. We all know you can create cool web applications Ruby on Rails, but what about all of the other cool things you can do with Ruby. So, I decided to put together a short list with summaries to help me remember why these projects are in my bookmarks. In no particular order:

Nanoc

Nanoc’s website describes it as “a tool that runs on your local computer and ‘compiles’ documents written in HTML, Markdown, Textile, Haml, etc. into a static web site, ready for uploading to any web server.” Basically, it takes your dynamic Ruby code and turns it into static HTML which you can then upload to your server, meaning you don’t necessarily need Ruby installed on your server. Of course, this depends on what your site actually needs to do. But I could see this as a perfect fit for a Blog, for which you write posts on your local machine and deploy. Of course you’d need to implement comments with a third-party javascript widget like Disqus, but that’s just one example.

Mongoid

Mongoid is a Ruby ODM framework for mapping your Ruby application to use MongoDB for object storage. If you haven’t checked out MongoDB as an alternative to using MySQL for your storage needs, it’s worth a look. You may not even realize you have different needs until you discover there’s a database out there to fit those needs. For another cool Database-Ruby integration alternative, see Friendly, a gem that makes MySQL look like a document-store to your application.

(more…)