Archive for the ‘Ruby’ Category

Named Routes in Rails

Thursday, December 1st, 2005

From Ronny Haryanto via the Rails Mailing List:

Interested in URLs that look something like this?

http://www.example.com/project/submarine

You can tell Rails to parse that URL in order to call the appropriate method on the controller.

 You might be interested in Named Routes.
http://wiki.rubyonrails.com/rails/pages/NamedRoutes

In config/routes.rb:

   map.project ‘project/:projtitle’ :controller => ‘project’,
                                    :action => ’show’

In app/controllers/project_controller.rb:

   def show
     @project = Project.find(:first,
                             :conditions => [’title = ?’,params[:projtitle]])
     …
   end

Or something like that. Assuming your project titles are unique. Then
you can also use project_url (since you have map.project in routes.rb)
for urls in your views, e.g.

   <% for project in @projects %>
   <%= link_to project.title,
               project_url(:projtitle => project.title) %>
    …
   <% end %>

Named Routes is a really cool feature, and one of the best of Rails,
I’d say.

Thanks Ronny!

Using Columns with Reserved Names in Rails

Thursday, December 1st, 2005

From Rails Mailing List : How to access column with a reserved name?

def new_column
  read_attribute 'old_column'
end

def new_column=(value)
  write_attribute 'old_column', value
end

Testing with Rails

Tuesday, November 29th, 2005

A hint with writing tests with Ruby on Rails.  If you are using fixtures, the name of the fixture is the name of the database table, not the name of the model under test.

For instance, this code:

class TaskTest < Test::Unit::TestCase
  fixtures :tbl_task

The symbol :tbl_task is the name of the database table.

ActiveOntology

Tuesday, November 29th, 2005

ActiveOntology is a library, written in Ruby, for easy manipulation of RDF and RDF-Schema models, thru a dynamic DSL based on Ruby idiom.


example = RDF::Resource.new "http://www.example.org/index.html"
example.namespaces < < :dc => “http://purl.org/dc/elements/1.1/”
example.dc_creator = RDF::Resource.new “http://www.example.org/staffid/85740″

Adjust Log Level in Ruby on Rails

Sunday, November 27th, 2005

To adjust your log level for a Rails project, you have two options.

  1. Edit config/environment.rb and set a global log level. This will override all environments (development, production, etc).
  2. Edit config/environments/production.rb, for instance to change production’s log level. This is recommended. Simply add the line config.log_level = :warn to set WARN level. This will greatly decrease the amount of logging performed.