Sending email from Rails application

It took me a while to configure sending email from Rails application. I went through many different tutorials, blogs, StackOverflow posts etc. Step, by step I found working configuration.

To send emails I use ActionMailer. First you need to generate mailer:

rails generate mailer UserMailer

It will create UserMailer class in app/mailers directory. In this class we need to define our method for sending emails:

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  def send_email(user_email, content)
  	@user_email = user_email
  	@content = content
  	mail(to: "your@mail.com", subject: "Email from mysite.com")
  end
end

Then in the directory app/views/user_mailer we need to create template for our email: send_email.html.erb (this name must match the name of action created in UserMailer class):

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <p>
      From: <%= @user_email %>
    </p>
    <p>
      <%= @content %>
    </p>
  </body>
</html>

(*) If you do not want to send html you can create plain text and name file send_email.text.erb.

Now, the hardest part. Configuration of smtp. You need to add it to config/environments/development.rb (or test.rb or production.rb). I found this configuration working for gmail:

# set delivery method to :smtp, :sendmail or :test
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true

  # these options are only needed if you choose smtp delivery
  config.action_mailer.smtp_settings = {
    :address        => 'smtp.gmail.com',
    :port           => 587,
    :domain         => 'gmail.com',
    :authentication => :login,
    :user_name      => 'your_gmail@gmail.com',
    :password       => 'your_password',
    :enable_starttls_auto => true
  }

The last thing is call ActionMailer from our app:

UserMailer.send_email(params[:from], params[:content]).deliver

Form for sending email can looks like that:

<%= form_tag '/send_email', method: 'post' do %>
	<div class="field">
		Email<br />
    <%= email_field_tag :from %>
  </div>

	<div class="field">
		Content<br />
    <%= text_area_tag :content, nil, rows: 10, cols: 25 %>
  </div>

  <div class="actions">
    <%= submit_tag "Send", class: "btn btn-large btn-primary" %>
  </div>
<% end %>

For that you need to configure action in controller and match route e.g.:

match '/send_email', to: 'your_controller#your_action'

Method send_mail (from UserMailer class) can have as much parameters as you need. It can be also 0. In above example, the params are just rendered in the email template (send_email.html.erb file).


Getting started with Ruby on Rails

Ruby on Rails logo

Recently I decided to learn Ruby on Rails. When you start learning a new technology you are always looking for the best available materials (to learn as efficient as possible). I did the same (using Google and StackOverflow). Fortunately I found The Best Way to Learn Ruby on Rails and I followed the recommended steps. With small modifications (extensions).

First of all I extended first step. Instead of just going through “Try Ruby” exercises I also read first chapter of Seven Languages in Seven Weeks (which is about Ruby). It was very good move, because this book is written in the way to show the flavors of language by comparison with others. I am a .NET guy, who was coding in PHP, C++, Java and Python before. Because of that I was more interested in the differences between Ruby and these languages, than in programming from ground up. I also reviewed (not read) Humble Little Ruby Book. It is a little bit more deep, but it gives you solid Ruby basics.

I was working with Rails on Windows and on Mac. Installing on Windows is very easy when you use RubyInstaller. There is also version for Mac. However on Mac you can also install Rails using RVM. In that case I recommend you installation screncast by Michael Hartl. On Windows I used RubyInstaller, but on Mac I took advantage of Michael Hartl’s screencast. Additionally you may also need SQLite Database Browser to browse your database easily. I did not know about it at the beginning and I was using rails dbconsole. Browsing with SQLite Database Browser is much more comfortable!

When I had Rails installed I went through Jeffrey’s Introduction to Rails. During that I tried to follow him, by writing code on my machine, but many times he was too fast. I needed to pausing video very often and even scrolling back to see written command (he was changing screens to quickly). Anyway it was very nice introduction and I recommend it. But you can skip rewriting and trying code he is writing. Just watch it to get a flavor of Rails.

After that I went through Rails for Zombies tutorials. I was very lucky, because Code School had promotion in May 18-19, and they provided Rails for Zombies 2 for free in these days. These tutorials are very solid. The exercises force you to learn by typing, because you cannot proceed to next level, until you do not finish all tasks.

Agile Web Development with Rails cover

With all basics gained as described above I started a book: Agile Web Development with Rails. I really like this book. It has 3 parts:

  • Getting started (Rails installation, create first app, quick Rails architecture overview)
  • Building application (tutorial)
  • Rails in Depth

The longest part of the book is the tutorial(2nd). Through this part you are creating an complete application exploring different Rails features. Unfortunately this book is a little bit outdated. Authors use ruby version 1.8.7 and Rails 3.0.0. I installed most recent versions: ruby 1.9.3p392 and Rails 3.2.13. Sometimes you need to fix the code (e.g. Chapter 11 – Task F: Add a Dash of Ajax). During that I found Ruby on Rails documentation very useful.

The last part is going deep into Rails. I really recommend this part! It is not only about Rails, but also about Web Applications and MVC architecture in general: how browser works, how requests are handled by Rails app, session, cookies etc.

When I was in the middle of book I was a little bit angry (because it is outdated) and I switched to Ruby on Rails tutorial by Michael Hartl, which is strongly recommended on StackOverflow. Well…guys at SO are right. This is really good piece of knowledge not only about Rails, but also about using git, css, Bootstrap and Web Development in general. I really enjoyed it! If you do not want to buy videos, you can just read the free book (it is the same content as in videos and more). There are also nice videos describing advanced setup for Rails development on Mac and SublimeText configuration for Rails. Actually this tutorial covers Rails development from A to Z.

As a summary to the book and Michael Hartl’s tutorial I reviewed Rails Guides. It is a nice overview for most important rails features. Can be also used as a reference. Some of the RailsCasts are also useful.

I wanted to try a few different tutorials/books to see different approaches. E.g. Michael Hartl use rspec for unit tests, but the authors of Agile Web Development with Rails are using rails testing framework.

My adventure with Ruby (on Rails) lasts almost two months. Now I can admit that ROR is a very powerful and developer friendly framework. It contains many features, which are already grabbed by ASP.NET (e.g. migrations, bundling). What was surprising for me, you do not need IDE to develop Rails apps. I use SublimeText2 (awesome editor!) and it is really enough. Some Rails developers use VIM or Emacs. Of course there are some IDEs such as RubyMine or Aptana Studio. I tried both. RubyMine seems to be pretty cool…but I stick with SublimeText. Additionally, during Rails development you spend lot of time with console (to create/run/undo migrations, create models/controllers, run tests etc.).

If you want to start Rails development, my recommended steps are:

You might also find these tools/resources useful:

What I like in Ruby on Rails? The syntax, convention over configuration and lots of implemented features in the framework layer. Moreover: Rails are just cool.


Build 2013

June was a month of conferences for .NET developers! We had Tech Ed North America, Norwegian Developers Conference, Tech Ed Europe and bunch more, but the biggest one was Microsoft Build Developer Conference.

All videos are live and available for free on Channel 9. Both Keynotes are worth to see, but for .NET people second day’s keynote would be more interesting. I also recommend Scott Hanselman‘s session What’s New in ASP.NET and Visual Studio 2013. Scott shows lots of nice new features like:

  • One ASP.NET
  • Bootstrap as default template in ASP.NET project
  • Multiple default browsers in Visual Studio
  • Real-Time website refreshing between Visual Studio and Browser (without page reloading)
  • New ninja snippets
  • and much, much more

There is also very solid session Introduction to Node.js on Windows Azure by Tomasz Janczuk. I really enjoyed this session. Tomasz shows Node.js basics, and how to it on Windows Azure in very simple and clear way.

Third must see (if you are .NET dev) is Visual Studio 2013 for Web Developers: Deep Dive by Mads Kristensen. Mads shows hot new features in VS editors (HTML, CSS, JavaScript) and WebEssentials add-on.

Other interesting stuff:


Tools I use everyday

I published a list of tools I use everyday. Maybe someone will find something what is useful for him, but never heard about.

I will try to keep it up to date and it will be always accessible through http://jj09.net/tools.

Not all tools are multi-platform. For now, the list is only for Windows. Maybe sometime in the future I will publish also “Mac Specific” tools.


Copy and paste without formatting

When you copy some piece of text e.g. from Web Browser to Word it preserves formatting. Which is sometimes (most of the time) unnecessary. I dealt with it (so far) with paste the text into notepad or url input in the browser and then copy again, and finally paste into destination. In case of Web Browser it was: CTRL+C (from source) -> CTRL+T (open new tab) -> CTRL+V (paste formatted text) -> CTRL+A (select entire text) -> CTRL+C (copy again, now it is not formatted) -> CTRL+W (close the tab) -> CTRL+V (paste not formatted text into destination).

I didn’t do that very often, so it wasn’t big issue for me. However recently I was working on some documentation and I was copying/pasting code from IDE into Word a lot. After n-th time I googled for ‘copy and paste without formatting’ and I found nice discussion on StackOverflow. One of recommended tools is PureText.

PureText

It allows to paste text from clipboard without formatting, by WIN+V. This shortcut can be changed in options.

My life is much easier right now. I am able to save around 5-10 seconds per each CTRL+C/WIN+V.