RubyOnRailsCheatSheet

From $1

Preferred Database Schema
  • Create tables named as plural nouns, lower-case.
  • Tables contain attributes named in lower-case.
  • Tables contain an auto-number attribute named "id"
  • If you are using MySQL, do NOT use hyphens in database or table names!

Creating CRUD Applet
  1. cd /railsapps

  2. rails appname

  3. cd appname
  4. Edit config/database.yml for your DBMS
  5. script/server

  6. Browse to http://server-name:3000

  7. ^C the server

  8. chmod -R 775 log public
  9. chown -R apache:kevin log public
  10. Create your models and controllers (Model-view-controller paradigm).
    • Use initial cap.
    • Use singular
    • Using ksh or bash...

   for f in Table1 Table2 Table3
   do
       script/generate scaffold $f
   done
  1. Test
    1. script/server
    2. http://server-name:3000/nounplural/list


Overriding the List Display
  • Create app/views/noun/list.rhtml similar to:

<html><head>
    <title>My Page Name</title>
</head>
<body>
<h1>My Title</h1>
<% if @nouns %>
    <table border="1" style="border-collapse: separate;">
        <tr>
            <td><b>ColumnTitle1</b></td>
            <td><b>ColumnTitle2</b></td>
        </tr>
        <% @nouns.each do |item| %>
            <tr>
                <td><%= link_to item.field1, :action => "show", :id => item.id %>&nbsp;</td>
                <td><%= item.field2 %>&nbsp;</td>
            </tr>
        <% end %>
    </table>
<% else %>
    <i>The table is empty.</i>
<% end %>
<p><%= link_to "Create new record", :action => "new" %></p>

</body></html>


But I Want To Use a Legacy Table Named 'LegacyTable' with the id field named LegacyKey, and Treat it Like a Table Named 'railsexamples'

  • When you generate models (and controllers)
  • script/generate model Railsexample
  • edit app/models/railsexample.rb and add

    def self.table_name()
        "LegacyTable"
    end
    def self.primary_key()
        "LegacyKey"
    end

Where Do I Tell Rails to Display More Than 10 Rows

  • See each controller in app/controllers.

Spiffy Hints

  • rake clone_structure_to_test

Where do I control page headers and page footers (page layout)?

  • See app/views/layouts/*.rhtml


See Also

Tags:
none
 
Images (0)
 
Comments (0)
You must login to post a comment.