Introduction to the rails platform for web application development.
Rails recently went to 2.0 and the jruby that comes with
netbeans does not seem to like it. Thus, ugliness:
- Install NetBeans 1.0.1. You might have to erase
~/.netbeans*
- Go to Tools->Plugins then update. Quit.
- Download and install jruby 1.0.3 [5].
- Start netbeans then create a new ruby on rails project:
- Select jRuby but set it to use the 1.0.3 binary.
- Update to rails 2.0
- Install JRuby openssl support.
- Select javaDB if you want to use derby (didn't work well for me: could not update).
- Copy
derby.jar
from glassfish
subdir to netbeans/ruby1/jruby/lib
(the
database.yml file will remind you of this step.)
orders |
id |
int |
customer_id |
int |
|
line_items |
id |
int |
order_id |
int |
|
customers |
id |
int |
name |
string |
|
addresses |
id |
int |
customer_id |
int |
|
class Order < ActiveRecord::Base
has_many :line_items
belongs_to :customer end
class LineItem < ActiveRecord::Base
belongs_to :order end
class Customer < ActiveRecord::Base
has_many :orders
has_one :address
end
class Address < ActiveRecord::Base
belongs_to :customer
end
myorder = Order.create(:customer_id => 33)
myorder.customer_id
myorder.name
Order.find(:all)
Company.find(:first, :conditions => [
"id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
{ :id => 3, :name => "37signals", :division => "First", :accounting_date => '2005-01-01' }
])
Student.find(:all, :conditions => { :grade => 9..12 })
products |
id |
int |
name |
string |
|
categories |
id |
int |
name |
string |
|
categories_products |
category_id |
int |
product_id |
int |
|
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
acategory.products aproduct.categories
- Join models using
:through
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
end
class Authorship < ActiveRecord::Base
belongs_to :author
belongs_to :book
end
@author = Author.find :first
@author.authorships.collect { |a| a.book } @author.books
/views/modelname/action.index.erb
- The
.erb
files are ruby templates.
- As in jsp, use
<%
for code, and <%=
to evaluate expressions.
<h1>Listing posts</h1>
<% for post in @posts %>
<h2><%=h post.title %></h2>
<p><%=h post.body %></p>
<p><%= link_to 'Show', post %> <%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></p>
<% end %>
<br/>
<%= link_to 'New post', new_post_path %>
link_to "text of link", :controller => 'categories', :action => 'list'
image_tag "spinner.png", :class => "image", :alt => "Spinner"
stylesheet_link_tak "scaffold", "admin", :media => "all"
<%
form_for :person, @person, :url => {:action => "update"},
:html => {:id => 'person_form'} do |f|
%>
First name: <%= f.text_field :first_name %>
Age: <%= f.text_field :age, :size => 2 %>
Bio: <%= f.text_area :biography %>
<% end %>
text_field :modelname, :attribute_name, options
password_field....
file_field
text_area
radio_button "model", "category", "rails"
<input type="radio" id="post_category" name="post[category]" value="rails"
checked="checked"/>
check_box "post", "validated" <input type="checkbox" id="post_validate" name="post[validated]"
value="1" checked="checked"/>
<input name="post[validated]" type="hidden" value="0"/>
select "post", "person_id", Person.find_all.collect {|p| [p.name, p.id]},
{:include_blank => true}
<select name="post[person_id]">
<option></option>
<option value="1" selected="selected">David</option>
<option value="2">Sam</option>
<option value="3">Tobias</option>
</select>
<html>
<head>
<title>Ajax Demo</title>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<h1>What time is it?</h1>
<div id="time_div">
I don't have the time, but
<%= link_to_remote( "click here", :update => "time_div",
:url =>{ :action => :say_when }) %>
and I will look it up.
</div>
</body>
</html>
On the server side the action is
class DemoController < ApplicationController
def index
end
def say_when
render_text "<p>The time is <b>" + DateTime.now.to_s + "</b></p>"
end
end
- If instead of replacing text you want to add it after just add
:position => "after"
<html>
<head>
<title>Ajax List Demo</title>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<h3>Add to list using Ajax</h3>
<%= form_remote_tag(:update => "my_list",
:url => { :action => :add_item },
:position => "top" ) %>
New item text:
<%= text_field_tag :newitem %>
<%= submit_tag "Add item with Ajax" %>
<%= end_form_tag %>
<ul id="my_list">
<li>Original item... please add more!</li>
</ul>
</body>
</html>
On the server, the controller says:
class ListdemoController < ApplicationController
def index
end
def add_item
render_text "<li>" + params[:newitem] + "</li>"
end
end
- You can also monitor a field and make an Ajax call if the user changes it:
Live Search: <%= text_field_tag :searchtext %>
<%= observe_field(:searchtext,
:frequency => 0.25,
:update => :search_hits,
:url => { :action => :live_search }) %>
<p>Search Results:</p>
<div id="search_hits"></div>