Wednesday, December 30, 2009

Don't use return statement in call block

return statement in call block returns from surrounding method instead of returning from call block.

Following code snippet describes it with example.

def function1(&block)

callback_result = yield('value1', 'value2')

puts "call block returned: #{callback_result}"

end

def function2()

function1() do |param1, param2|
puts "block implementation of function2 with param1 => #{param1} & param2 => #{param2}"
return "#{param1} changed. #{param2} changed."
end

puts "next line after calling function1"

return "function2 returned"
end


def function3()

function1() do |param1, param2|
puts "block implementation of function3 with param1 => #{param1} & param2 => #{param2}"
"#{param1} changed. #{param2} changed."
end

puts "next line after calling function1"

return "function3 returned"
end

output of function2() is

block implementation of function2 with param1 => value1 & param2 => value2
"value1 changed. value2 changed."

Output of function3() is

block implementation of function3 with param1 => value1 & param2 => value2
call block returned: value1 changed. value2 changed.
next line after calling function1
"function3 returned"

Tuesday, April 28, 2009

Layout in actionmailer

By default mailer methods expects layout in view/layouts directory by the underscored version of the mailer class name.

If mailer class is like

class UserMailer <
ActionMailer::Base

def welcome_mail()

....
end

....
end


then the mailer layout name will be user_mailer.html.erb in view/layouts directory. The layout name needs to end in “_mailer” to be automatically recognized by mailer as a layout.

Content of user_mailer.html.erb should contain yield like the application layout. In this example the welcome_mail.html.erb will be rendered at yield position.

A different layout can also be set using

layout 'awesome' # it will use awesome.html.erb as the layout


class UserMailer < ActionMailer::Base

layout 'awesome'

....
end


Resource: http://guides.rubyonrails.org/action_mailer_basics.html

Wednesday, April 22, 2009

Error in loading mysql library in rails

Problem:

Error in loading mysql library

The specified module could not be found. - C:/ruby/lib/ruby/gems/1.8/gems/mysql-2.7.3-x86-mswin32/ext/mysql.so

Solution:

For this to work, you must have libmysql.dll in your PATH environmental variable. If you have MySQL installed locally,
just make sure that \bin is in your path. If you don't have MySQL installed locally, you can
install one or more of the MySQL tools, find the libmysql.dll included in their bin directory, and copy it to
the %SystemRoot%\System32 folder.

Reference: RubyInstallDirectory/lib/ruby/gems/1.8/gems/mysql-2.7.3-x86-mswin32/README

Saturday, January 10, 2009

Access a method from both view and controller

If you need a method that will be accessed from both view and controller write the method in controller and pass the name of the function as an argument of helper_method.

class ApplicationController < ActionController::Base

..

..

def logged_in_user()
    user_id = session[:user_id]
    user = User.find_by_id(user_id)
    return user
  end

helper_method :logged_in_user

end

Here, logged_in_user method is accessible from all of the controllers those extend ApplicationController and helper_method :logged_in_user will expose this method to views. Your common accessible method signature may have arguments. But, only helper_method :logged_in_user is enough to make it accessible from view with the same signature defined.

Sunday, November 30, 2008

linux password

passwd Change your own password.

passwd sleepy Change sleepy's password.

passwd -d sleepy Delete sleepy's password.

reference

http://lowfatlinux.com/linux-change-password-passwd.html