Ruby On Rails 
top

GEM

Gem # Ruby Ruby notes

Other Rails questions

Why I cannot change the top page on Rails? How to output a debug log in Rails Percent minus and minus percent notation How to set head tag content dynamically? I'm getting NameError for setting up a before_filter. Why?

Sharing a partial html in Rails

Use Partial. Partials are partial templates and can be rendered as a part of a template:

<%= render :partial => "localsearch/searchbox", :locals => { :mycaller => "I'm calling" } %>

Create a database column with not null characteristic

You can create it with add_column with :null => false option.

add_column :users, :superuser, :boolean, :null => false

Rename a column

Use rename_column.

Change the column type

Use change_column in migration.

How to output a debug log?

Use logger.debug() in a controller.

class IsbnController < ApplicationController
  def index
    logger.debug(param)
  end

How to generate a model?

This doesn’t create fields.

% script/generate model <i>Item</i>
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/item.rb
      create  test/unit/item_test.rb
      create  test/fixtures/items.yml
      create  db/migrate
      create  db/migrate/20081121091705_create_items.rb

Add a field to a model

Summary 1. Generate a migration file. 2. Modify the generated migration file to have desired table changes. 3. Apply migration file.

  1. Generate a migration file. % script/generate migration somename exists db/migrate create db/migrate/20081121101345_somename.rb

The generated file (20081121101345_somename.rb) is empty.

class Somename < ActiveRecord::Migration
  def self.up
  end

  def self.down
  end
end
  1. Fill self.up and self.down methods.
class Shopitem < ActiveRecord::Migration
  def self.up
        add_column :shopitems, :bought, :boolean
  end

  def self.down
        remove_column :shopitems, :bought
  end
end
  1. Apply migraiton file with rake db:migrate command.

Alternatively you can use special directives such as add_FIELD_to_TABLE.

script/generate add_name_to_item name:string
  • Rubyによるデータベースをバックエンドとしたウェブアプリ用のフレームワーク
  • 生産性がとても高いらしい
  • メタプログラミングフレームワーク(って何?) 略記はRoR
  • Railsで実現されていることは、highly dynamic, dynamically typed languagesで実現可能。

How to get id parameter of an url?

Use params[:id].

#show action
def show
  logger.debug('show %d' % params[:id])
end

# How to change a column type used by a rails model? change_column

script/generate migration gpxfile db/migrate/002_gpxfile.rb is created.

migration name should be something descriptive, but doesn’t need to be a table name.

imported