Tweetegy On the edge of chaos with Ruby, Rails, JavaScript and AngularJS.

| About | Search | Archive | Github | RSS |

Migrate a PHP application to Rails 3

I recently migrated a basic PHP website to Rails 3. The job went smoothly and this is my first Rails 3 app so I thought I would write about my experiences.

Getting Started

In this situation I would start directly at database level. Get the database ready so that you can create simple ActiveRecord classes without any rule bending. Here are some tips:

Note:

Because you are building this inside out so to speak (i.e. not using generators) you might come across issues like wanting to use reserved words for your models. in my case I needed to name a model Class (as in a taught class) but had to rename to Klass and use set_table_name as follows:

1 class Klass < ActiveRecord::Base
2   set_table_name "classes"
3 end

Date Formatting in Rails 3

Simply create a file under config/initializers/ directory called time_formats.rb and include formats that you might be needing throughout your application. in my case I wanted dates to be output as Full day, Day number, Month, Year e.g. Monday, 5th April 2011

1 Date::DATE_FORMATS[:schedule_date] = "%A %d %B %Y"

Rendering Collections with Partials in Rails 3

Something else that is quite useful is rendering partials with collections. By simply passing a collection to the render method, Rails will automatically render a partial with the singular name from the models view directory.

For example, I have a list of courses under each category that I want to render, so by passing category.courses the view views/course/_course.html.erb is rendered for each course item in the collection. Marvelous!

1 <%= render category.courses %>

Select drop down lists in Rails 3

Making select drop downs is amazingly clear and simple also. I made use of options_from_collection_for_select which is a very neat and powerful way to generate a set of options from an existing collection. Used in conjunction with select_tag renders our completed drop down list of options!

1 <%= select(:enrollment, :class_id, options_from_collection_for_select(@klasses, :id, :name)) %>

Showing users error notifications in Rails 3

For some reason, the form builder helpers error_messages have been removed from Rails 3. I presume it is to allow the developer more control over rendering any validation errors. So if you want to output errors, use the following example to guide you:

 1 <%= form_for(@enrollment) do |f| %>
 2 <% if @enrollment.errors.any? %>
 3   <ol>
 4     <% @enrollment.errors.full_messages.each do |error_msg| %>
 5     <li>
 6       <%= error_msg %>
 7     </li>
 8     <% end %>
 9   </ol>
10 <% end %>

SEO Friendly urls

SEO Friendly urls are really easy to setup so put this in yer model and smoke it! The to_param method is used by the url helpers so by overriding in this way autmatically generates the url you choose with no further fuss!

1 def to_param
2   "#{id}-#{title.parameterize}"
3 end

Sitemap XML file generation example in Rails 3

Sitemap XML file generation, I recommend: Sitemap Generator for Rails

Note to get this working you need to add the following include to your rake task

1 include Rails.application.routes.url_helpers

...and also the following configuration change to your application.rb config

1 config.autoload_paths += %W(#{config.root}/lib)

Conclusion

I am very happy that I moved my site to Rails 3 from PHP. It's much cleaner now, easier to maintain and fits in with my own personal desire to focus on building applications with Rails!