Member Route error "No action responded to"

Problem:

Member routes allow one to create additional routes to existing resources. For example, if you want to add an approve path to your purchases resource, you could do this in your config/routes.rb:

 map.resources :purchases, :member => { :approve => :put }

My problem was that the link I made for approving the purchase

<%= link_to "Approve Purchase", approve_purchase_path(@purchase), :confirm => "Proceed with approval?" %>

resulted in the following error:

Unknown action

No action responded to 1. Actions: approve, create, destroy, edit, index, new, show, and update

Cause:

Rails confused the link with a normal get operation.

Solution:

Explicitly declare the HTTP method for the action.

<%= link_to "Approve Purchase", approve_purchase_path(@purchase), :confirm => "Proceed with approval?", :method => :put %>

HTML Line Breaks in Rails Text Fields

Problem:

Rails doesn’t automatically convert newline (“\n“) characters in strings to line breaks (“<br>“) with h.

<%= h @user.address %>

Solution:

Manually replacing the \n with <br> without using h will make your site vulnerable to XSS attacks. The proper approach would be to convert the line breaks after h. Note that Rails already has a method, simple_format, that converts line breaks.

<%= simple_format(h @user.address) %>

Rails Development Server is Slow

Problem:

When developing for Ruby on Rails, the development server (the one used by script/server) feels slow and unresponsive.

Cause:

RoR uses WEBrick as the default server. Based on my experience on a Ubuntu virtual machine with 1GB of RAM, this server doesn’t respond as fast as the web application servers in other web languages (Apache, Tomcat, IIS).

Solution:

Install Mongrel. In Ubuntu 9.04, you can install this using:

sudo gem install mongrel

In case you receive build errors, you might have to install ruby-dev to deal with the dependency issues.

sudo apt-get install ruby-dev

Once installed, Rails will automatically use Mongrel as the application server over WEBrick.

UPDATE: Mongrel’s pretty much dead by now. Use thin instead.

Just add gem 'thin' to your Gemfile and run bundle install.

Inject/Reduce doesn’t work on lists of ActiveRecords

Problem:

The following line of code for calculating the total order cost returns “#”.

@purchase_items.reduce() {|cost, item| cost += item.qty_ordered * item.unit_price }

Cause:

The default memo item for a reduce operation in Ruby is the first item on the list (because of this, it starts the reduce on the second item).

Solution:

You can set the initial memo item by passing it to the reduce function.

@purchase_items.reduce(0) {|cost, item| cost += item.qty_ordered * item.unit_price }

APC UPS Battery Replacement

UPS serviced by ClixLogic, Inc

Problem:

APC UPS battery needs replacement

Cause:

Umm.. because it’s already past its lifespan? The biggest sign that it needed replacement that day was when the UPS won’t turn on after dying.

No, wait, the biggest sign was the fact that I had to pry open the battery cover because the battery already expanded like those bulging MacBook Pro batteries two years ago.

Solution:

ClixLogic, Inc. handles the servicing of APC products in the Philippines.

Their Manila office is Brgy. Holy Spirit in Quezon City so for people living in southern part of Metro Manila, bringing and retrieving a heavy device all the way there can be difficult. Fortunately, they have a closer drop-off point, Signal Telecom, at V-Mall Greenhills.

Here’s how I got my UPS’s battery replaced:

  1. I brought my UPS to Signal Telecom at the 3rd floor of V-Mall and told them I was there to have the battery replaced. They took the device, gave me an order slip/receipt, and told me to call ClixLogic informing them of my service request. I didn’t pay anything at this point.
  2. Called ClixLogic. They said they’ll pick it up from Signal Telecom the next time they go there (usually Fridays).
  3. Once they saw the UPS in Signal Telecom, a ClixLogic employee texted me their quotation for the battery replacement (1.8k for a BK650-AS RB17 battery) and asked for confirmation of the job. Obviously, I gave them the go signal.
  4. After about 3 weeks of no response, I followed up my service request by calling their office last Friday. I don’t know if there was a mix-up involved, but regardless, they said they’ll deliver the UPS to Signal Telecom on Monday.
  5. A guy from Signal Telecom texted me today that my UPS is now with them.
  6. I went to Signal Telecom, showed them my order slip, paid the 1.8k, and got my UPS back.

I’m not good with RMAs or the like so depending on your diskarte, you might be able to get your battery replaced with less hassle.

Next up, RMA-ing a Seagate hard drive.