firstruby

Master ruby with us

Archive for February, 2007

How to Send email through Ruby On Rails application

Posted by firstruby on February 28, 2007

Configuration to send email through our Ruby On Rails application

1) My UserNotify.rb model with signup method as follows,

class UserNotify < ActionMailer::Base
  def signup(user, password, url=nil)
  setup_email(user)# Email header info
  @subject += “Welcome to #{UserSystem::CONFIG[:app_name]}!”# Email body

substitutions
  @body[“name”] = “#{user.firstname} #{user.lastname}”
  @body[“login”] = user.login
  @body[“password”] = password
  @body[“url”] = url || UserSystem::CONFIG[:app_url].to_s
  @body[“app_name”] = UserSystem::CONFIG[:app_name].to_s
  end

2) Call to signup method of model from controller UserController.rb is,

UserNotify.deliver_signup(@user, @params[‘user’][‘password’], url)
3) Create template to send with email as /user_notify/welcome.rhtml

 Welcome to <%= @app_name%>, <%= @name%>.

Your login credentials are:

login: <%= @login %>password: <%= @password %>

Please click on the following link to confirm your registration:

<a href=”<%= @url%>”>Click me!</a>

<%= @url %>

4) You also need to add the following at the end of your config/environment.rb file,

require ‘environments/user_environment’

5) Also, you must properly configure ActionMailer for your mail settings. For example, I have the following in config/environment.rb

ActionMailer::Base.server_settings = {

:address => “IP address”,        # Enter your public IP address here

:port => 25                                   # Specify your SMTP port

}

6) Create  ‘environments/user_environment.rb’ file as,

module UserSystem
  CONFIG = {
    # Source address for user emails
    :email_from => ‘craig@gmail.com’,

    # Destination email for system errors
    :admin_email => ‘craig@gmail.com’
}
end

 —————————————————————————–

 This is all to send email through Ruby On Rails application. Check the email you have entered at the time of signup.

Fortunately, you won’t experience issues. I may forget some parts or putting some steps that are not exactly in accordance with your system configuration. Please forgive me if I made some mistakes, I’m no “RubyOnRails guru”
I’m ready to hear comments, suggestions or troubles !

Advertisements

Posted in Useful Links | Leave a Comment »

Ruby Gems

Posted by firstruby on February 4, 2007

Using Ruby Gems

You may want to add functionality to your application using Ruby Gems. A gem is a packaged Ruby application or library. A great introduction to Ruby Gems can be found here. Basically, Ruby Gems keep you from having to reinvent the wheel, by providing you with modular pieces of code that can be accessed from within your application’s code.

By default, some gems will come pre-installed with your hosting plan. Generally these are Rails-relevant gems (ActiveRecord, ActionMailer, etc.). To see a list of installed gems at any time, type “gem list –local” from an SSH window.

To install new gems, issue the command “gem install <gem-name>” (where <gem-name> is the name of the gem you wish to install). This command will first search your local machine for the specified gem, and if not found, will attempt to automatically download the gem from RubyForge, an online gem repository. Check RubyForge for a complete listing of available gems or check the link here for a more condensed listing. More installation options can be found by typing “gem install –help”.

By default, gems will be installed to ‘/usr/lib/ruby/gems/1.8/gems/’.

Freezing Ruby Gems

Whenever a new gem version appears on the host, your Rails application will attempt to automatically upgrade to that version. This is convenient when in development, but after moving to production, it can cause things to break unexpectedly. For obvious reasons, you don’t necessarily want your application being updated after it has been published. To prevent gems from updating, change directories to your application root, and issue the command “rake freeze_gems”. This copies the gems from /usr/lib/ruby/gems/1.8/gems/ (the default location) to <your-app-name>/vendor/ and keeps them from being automatically updated.There is one caveat with the “rake freeze_gems” command. It will only freeze gems that are Rails-relevant. If running this command doesn’t freeze the gem(s) you want it to, (i.e. they are not found in <app-name>/vendor/) you will need to use the solution found here.
This section is intended only to give a brief introduction to concept of freezing gems. If this is something you are interested in doing, it is highly recommended that you read up on the documentation at http://www.rubygems.org for more information.

Posted in Blogroll, firstruby, Useful Links | Leave a Comment »