Controllers

Note: redirect_to

redirect_to admin_destinations_path( :country_id => @destination.country_id )

Note: Creating a controller under Admin namespace

$ rails generate model Country
$ rails g controller Admin::Countries
      create  app/controllers/admin/countries_controller.rb
      invoke  erb
      create    app/views/admin/countries
      invoke  rspec
      create    spec/controllers/admin/countries_controller_spec.rb
      invoke  helper
      create    app/helpers/admin/countries_helper.rb
      invoke    rspec
      create      spec/helpers/admin/countries_helper_spec.rb

Note: Change class Admin::CountriesController < ApplicationController to use

class Admin::CountriesController < Admin::ApplicationController
  def new
    @country = Country.new
  end
end
 
class Admin::ApplicationController < ApplicationController
  before_filter :authenticate_user!
 
  layout 'admin'  
end

Models

Note: Validation in Rails3

  #(rails2):
  #validates_presence_of :country_name
 
  #(rails3):
  validates :country_name, :presence => true

Note: My foreign key does not save?
When you have an association between models usually defined by foreign keys and sometimes it does not update, make sure you add the key in the model’s :attr_accessible

attr_accessible :destination_name, :description, :country_id, :destination_type_id

SEO Friendly URLS
http://www.seoonrails.com/to_param-for-better-looking-urls.html
http://nithinbekal.com/2010/03/01/rails-seo-friendly-urls-using-to_param/

class Poi < ActiveRecord::Base
  def to_param
    "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
  end
end

In the view, its still the same:

<%= link_to poi.name, poi %>

Views

Note: Deleting a model from a view from a REST route

This is dependent on prototype or jquery.rails.js

<li><%= link_to "Destroy", admin_poi_path, :confirm => 'Are you sure?', :method => :delete %></li>

The “:delete” is a symbol of HTTP verb (POST, DELETE, PUT). :method => symbol of HTTP verb – This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. If :href => ‘#’ is used and the user has JavaScript disabled clicking the link will have no effect. If you are relying on the POST behavior, you should check for it in your controller’s action by using the request object’s methods for post?, delete? or put?.

              admin_pois GET    /admin/pois(.:format)                  {:action=>"index", :controller=>"admin/pois"}
                         POST   /admin/pois(.:format)                  {:action=>"create", :controller=>"admin/pois"}
           new_admin_poi GET    /admin/pois/new(.:format)              {:action=>"new", :controller=>"admin/pois"}
          edit_admin_poi GET    /admin/pois/:id/edit(.:format)         {:action=>"edit", :controller=>"admin/pois"}
               admin_poi GET    /admin/pois/:id(.:format)              {:action=>"show", :controller=>"admin/pois"}
                         PUT    /admin/pois/:id(.:format)              {:action=>"update", :controller=>"admin/pois"}
                         DELETE /admin/pois/:id(.:format)              {:action=>"destroy", :controller=>"admin/pois"}

Note: Displaying in_groups_of vertical

http://railscasts.com/episodes/28-in-groups-of

<table cellpadding="2" cellspacing="2">
  <% @destinations.in_groups_of((@destinations.length/3).ceil).transpose.each do |destinations| %>
    <tr>
      <% destinations.each do |destination| %>
        <td>
            <%= render destination %>
        </td>
      <% end %>
    </tr>
  <% end %>  
</table>
 
<% @destinations.in_groups(5, false) do |group| %>
  <ul class="column_list">
    <%= render :partial => "destinations/destination", :collection => group %>
  </ul>
<% end %>

Others

Migrate a mysql to sqlite database

  1. Install yaml_db.git from

http://www.kartar.net/2008/08/migrating-a-rails-database-from-sqlite3-to-mysql/

  1. Have the mysql database ready. We will configure it on production.

  2. config/database.yml

development:
  adapter: sqlite3
  database: db/mytp-1.0.4.db
  pool: 5
  timeout: 5000
 
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: mysql
  database: tp_production
  username: rupert
  password: ******
  host: localhost
  1. Dump the database from production with rake command below. We should have as output: data.yml
    rake db:dump RAILS_ENV=production

  2. Load data.yml to SQLite. Ensure you have given a database name for sqlite. We should have as output: mytp-1.0.4.db under db/
    rake db:load RAILS_ENV=development

Adding ar_mailer to Rails

  1. environment.rb:
  config.gem "adzap-ar_mailer", :lib => 'action_mailer/ar_mailer'
  config.action_mailer.delivery_method = :activerecord
#production mailing settings
ActionMailer::Base.smtp_settings = {
   :address => "127.0.0.1",
   :port => 25,
   :domain => "mytravelphilippines.com",
   :authentication => :login,
   :user_name => "info",
   :password => "******",
}
 
ActionMailer::Base.default_content_type = "text/plain"
  1. Emailer.rb is still
class Emailer < ActionMailer::Base
  1. Run
rupert@2rmobile:/opt/rails/tp/current$ ar_sendmail -ov
expired  emails from the queue
found 1 emails to send
sent email 00000000002 from info@mytravelphilippines.com to rupert@2rmobile.com: #<Net::SMTP::Response:0xb702279c @string="250 Message accepted.\n", @status="250">