<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=Rails_Basics</id>
	<title>Rails Basics - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=Rails_Basics"/>
	<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=Rails_Basics&amp;action=history"/>
	<updated>2026-04-24T17:25:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://wiki.scott5.org/index.php?title=Rails_Basics&amp;diff=279&amp;oldid=prev</id>
		<title>Scott: /* Application Setup */</title>
		<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=Rails_Basics&amp;diff=279&amp;oldid=prev"/>
		<updated>2011-02-02T19:28:32Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Application Setup&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Installation ==&lt;br /&gt;
* Install ruby and rubygems as you would any other package.&lt;br /&gt;
* Use gems to install rails: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo gem update&lt;br /&gt;
sudo gem install rails&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* To match a specific version:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem install rails -v 2.3.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* show installed gems:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem list&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* make links to /var/lib/gems/1.8/bin binaries in /usr/local/bin&lt;br /&gt;
&lt;br /&gt;
== More Installation Wisdom ==&lt;br /&gt;
I wanted to be able to develop this on my macbook too. I installed ruby and rubygems from source. I ended up getting version 1.3.1 of rubygems and 2.2.2 of rails, which was higher than on my Ubuntu Intrepid machine. So I had to update rubygems beyond the official intrepid version using this: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo gem install rubygems-update&lt;br /&gt;
cd /var/lib/gems/1.8/bin&lt;br /&gt;
sudo ./update_rubygems&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now I have to reinstall the latest gems all over again: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo gem install rails composite_primary_keys&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There were also problems because rails has dropped default support for mysql: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo apt-get install ruby1.8-dev libmysqlclient15-dev&lt;br /&gt;
sudo gem install mysql&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edits to &amp;lt;code&amp;gt;config/environment.rb&amp;lt;/code&amp;gt;:&lt;br /&gt;
* comment out this line: &amp;lt;code&amp;gt;config.time_zone = &amp;#039;UTC&amp;#039;&amp;lt;/code&amp;gt;&lt;br /&gt;
* add this line: &amp;lt;code&amp;gt;require &amp;#039;composite_primary_keys&amp;#039;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copied old &amp;lt;code&amp;gt;config/database.yml&amp;lt;/code&amp;gt; to new project &lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
* Ruby on Rails web site: http://www.rubyonrails.com/&lt;br /&gt;
* Documentation: http://api.rubyonrails.org/,&lt;br /&gt;
* Wiki: http://wiki.rubyonrails.com/&lt;br /&gt;
&lt;br /&gt;
== Application Setup ==&lt;br /&gt;
* Create rails app: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails -d mysql cookbook&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This creates a big directory containing all code for the project. &lt;br /&gt;
&lt;br /&gt;
* Login to mysql as root and create mysql databases: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
mysql&amp;gt; create database cookbook_dev;&lt;br /&gt;
mysql&amp;gt; create database cookbook_test;&lt;br /&gt;
mysql&amp;gt; create database cookbook_prod;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a mysql user account and give it rights to the databases: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
mysql&amp;gt; grant all privileges on cookbook_dev.* to &amp;#039;rails_user&amp;#039;@&amp;#039;localhost&amp;#039; identified by &amp;#039;r8!lz&amp;#039;;&lt;br /&gt;
mysql&amp;gt; grant all privileges on cookbook_test.* to &amp;#039;rails_user&amp;#039;@&amp;#039;localhost&amp;#039; identified by &amp;#039;r8!lz&amp;#039;;&lt;br /&gt;
mysql&amp;gt; grant all privileges on cookbook_prod.* to &amp;#039;rails_user&amp;#039;@&amp;#039;localhost&amp;#039; identified by &amp;#039;r8!lz&amp;#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Define tables. Edit a file called create-mysql-db.sql:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
drop table if exists &amp;#039;chapters&amp;#039;;&lt;br /&gt;
create table chapters (&lt;br /&gt;
    id                             int not null auto_increment,&lt;br /&gt;
    title                          varchar(255) not null,&lt;br /&gt;
    sort_order                     int not null default 0,&lt;br /&gt;
        primary key (id)&lt;br /&gt;
) type=innodb;&lt;br /&gt;
&lt;br /&gt;
drop table if exists &amp;#039;recipes&amp;#039;;&lt;br /&gt;
create table recipes (&lt;br /&gt;
    id                             int not null auto_increment,&lt;br /&gt;
    chapter_id                     int not null,&lt;br /&gt;
    title                          varchar(255) not null,&lt;br /&gt;
    problem                        text not null,&lt;br /&gt;
    solution                       text not null,&lt;br /&gt;
    discussion                     text not null,&lt;br /&gt;
    see_also                       text null,&lt;br /&gt;
    sort_order                     int not null default 0,&lt;br /&gt;
        primary key (id, chapter_id, title),&lt;br /&gt;
        foreign key (chapter_id) references chapters(id)&lt;br /&gt;
) type=innodb;&lt;br /&gt;
&lt;br /&gt;
drop table if exists &amp;#039;tags&amp;#039;;&lt;br /&gt;
create table tags (&lt;br /&gt;
    id                             int not null auto_increment,&lt;br /&gt;
    name                           varchar(80) not null,&lt;br /&gt;
        primary key (id)&lt;br /&gt;
) type=innodb;&lt;br /&gt;
&lt;br /&gt;
drop table if exists &amp;#039;recipes_tags&amp;#039;;&lt;br /&gt;
create table recipes_tags (&lt;br /&gt;
    recipe_id                      int not null,&lt;br /&gt;
    tag_id                         int not null,&lt;br /&gt;
        primary key (recipe_id, tag_id),&lt;br /&gt;
        foreign key (recipe_id) references recipes(id),&lt;br /&gt;
        foreign key (tag_id)  references tags(id)&lt;br /&gt;
) type=innodb;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create tables: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ mysql cookbook_dev -u rails_user -p &amp;lt; create-mysql-db.sql&lt;br /&gt;
$ mysql cookbook_test -u rails_user -p &amp;lt; create-mysql-db.sql&lt;br /&gt;
$ mysql cookbook_prod -u rails_user -p &amp;lt; create-mysql-db.sql&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Edit &amp;lt;code&amp;gt;config/database.yml&amp;lt;/code&amp;gt; to have the right username/password for each database (double-check database names too).&lt;br /&gt;
&lt;br /&gt;
== Model ==&lt;br /&gt;
To create an object that will be linked to a database table (e.g. users): &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby script/generate model &amp;lt;object/table name (singular form)&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby script/generate model user&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a file called &amp;lt;code&amp;gt;app/models/user.rb&amp;lt;/code&amp;gt;, which defines a User ActiveRecord class. Edit this file to add table relationships. For example, we might have &amp;lt;code&amp;gt;cookbook/app/models/chapter.rb&amp;lt;/code&amp;gt;: &lt;br /&gt;
&amp;lt;source lang=&amp;quot;rails&amp;quot;&amp;gt;&lt;br /&gt;
class Chapter &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Other relationship functions include &amp;lt;code&amp;gt;belongs_to&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;has_and_belongs_to_many&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Find example:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;rails&amp;quot;&amp;gt;&lt;br /&gt;
found_user = User.find(userid)&lt;br /&gt;
found_user = User.find(:first, :conditions =&amp;gt; [&amp;quot;email = ?&amp;quot;, email])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Controller ==&lt;br /&gt;
To control an object&amp;#039;s behavior: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby script/generate controller &amp;lt;object (single form)&amp;gt; &amp;lt;list of actions&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For example: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby script/generate controller user add delete login logout&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This command creates a file &amp;lt;code&amp;gt;app/controllers/user_controller.rb&amp;lt;/code&amp;gt;, which defines a class &amp;lt;code&amp;gt;UserController&amp;lt;/code&amp;gt;. The class defines four stub methods: &amp;lt;code&amp;gt;add&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;delete&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;login&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;logout&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Logging from a controller ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;rails&amp;quot;&amp;gt;&lt;br /&gt;
class HomeController &amp;lt; ActionController::Base&lt;br /&gt;
  def index&lt;br /&gt;
    logger.info &amp;#039;informational message&amp;#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This will show up in the server log. Log levels are debug, info, warn, error, and fatal. In production, debug entries are omitted. &lt;br /&gt;
&lt;br /&gt;
== View ==&lt;br /&gt;
The view files are created automatically for each action specified at controller creation. The same command that created the &amp;lt;code&amp;gt;UserController&amp;lt;/code&amp;gt; class above also created four files in &amp;lt;code&amp;gt;app/views/user/&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;add.rhtml&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;delete.rhtml&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;login.rhtml&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;logout.rhtml&amp;lt;/code&amp;gt;. A user accesses the view by opening &amp;lt;code&amp;gt;http://www.example.com/my_app/add&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Info on &amp;lt;code&amp;gt;select&amp;lt;/code&amp;gt; vs &amp;lt;code&amp;gt;select_tag&amp;lt;/code&amp;gt; vs &amp;lt;code&amp;gt;collection_select&amp;lt;/code&amp;gt;: http://shiningthrough.co.uk/blog/show/6&lt;br /&gt;
&lt;br /&gt;
=== Editing .rhtml files ===&lt;br /&gt;
* Use &amp;lt;code&amp;gt;&amp;lt;% ... %&amp;gt;&amp;lt;/code&amp;gt; to hold ruby code that should be processed.&lt;br /&gt;
* Use &amp;lt;code&amp;gt;&amp;lt;%= ... %&amp;gt;&amp;lt;/code&amp;gt; to also print the output of the code.&lt;br /&gt;
* Comments: &lt;br /&gt;
&amp;lt;source lang=&amp;quot;rails&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;%&lt;br /&gt;
=begin&lt;br /&gt;
This style will work with scriplets too  &amp;lt;%= now.broken.function(foo) %&amp;gt;&lt;br /&gt;
=end&lt;br /&gt;
%&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% if false %&amp;gt;&lt;br /&gt;
so will this style &amp;lt;%= now.broken.function(foo) %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access to data ===&lt;br /&gt;
* The view can access the controller&amp;#039;s instance variables like &amp;lt;code&amp;gt;@name&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The view can&amp;#039;t access any of the controller&amp;#039;s methods, class variables (&amp;lt;code&amp;gt;@@my_class_name&amp;lt;/code&amp;gt;), or constants.&lt;br /&gt;
* The view can access global constants defined in &amp;lt;code&amp;gt;config/environment.rb&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
=== Layouts/Templates ===&lt;br /&gt;
* To apply a layout to every page in your web application, create a file called &amp;lt;code&amp;gt;app/views/layouts/application.rhtml&amp;lt;/code&amp;gt;. It should look something like this: &lt;br /&gt;
&amp;lt;source lang=&amp;quot;rails&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;My Website&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;%= @content_for_layout %&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
* Make a view-specific template by creating &amp;lt;code&amp;gt;app/views/layouts/my_view.rhtml&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Layouts can access all of the same data that views can.&lt;/div&gt;</summary>
		<author><name>Scott</name></author>
	</entry>
</feed>