Showing posts with label RoR. Show all posts
Showing posts with label RoR. Show all posts

Tuesday, August 13, 2013

Ruby -=- undefined method `[]' for #Enumerable::Enumerator

Repost from Yann Laviolette blog. I got similar problem and his solution works.

Below is his instruction:

When I upgrade to Ruby 1.8.7 I've got some problems. I'm getting the error message undefined method `[]' for #Enumerable::Enumerator.

The problem occurs with the version 1.8.7 of Ruby and it's supposed to be corrected in the version 9. To fix this problem you can either downgrade to Ruby 1.8.6 OR put this piece of code in your environnement.rb within initializer block:

 unless '1.9'.respond_to?(:force_encoding)  
  String.class_eval do  
   begin  
    remove_method :chars  
   rescue NameError  
    # OK  
   end  
  end  
 end  


Sunday, August 4, 2013

Rails 2 debug logger


I need to have clean log for a Rails 2 app. So follow the guide to change the environment.rb:

Rails::Initializer.run do |config|

config.log_level = :debug

From myController:
logger.debug "Debug some thing"

But it didn't work. There's no log in the development.log!

Then switch to 
config.log_level = :warn

From myController:
logger.warn "This works!"

Worked! Don't know why? But that's it.

Saturday, July 27, 2013

Rails helpers

I have a task to fix a conversion bug in a Rails app (2.12), then I found out that there are copies of the same method in everywhere - that's so bad. So I decided to clean it up by making a common helper that provides common methods for global usage, like below example:

 module UtilsHelper  
  def convertValue(value)  
   return "0" if value.nil?  
   return (value*2).to_s  
  end  
 end  

Google around, finally get it to run:

In the Controller, use helper method to include UtilsHelper to use in the template:
 class HomeController < ApplicationController  
  helper UtilsHelper #include won't work  
 end  

index.rhtml template:
 <%= convertValue(row.value) %>  

But in a class, we must use include method
 class Data  
  include UtilsHelper #require won't work  
  @value = nil  
  def to_s  
   return convertValue(@value)  
  end  
 end