here is how we do it from scaffolding:
Symbols and Strings
<%= f.label :seat_id_seq %> This is a symbol. They always start with a colon. Generally used to name things. Mostly interchangeable with strings.
<%= f.label “Seat #” %> This is a string.
Add a column to the DB
to add the database we:
ruby script/generate scaffold ticket name:string seat_id_seq:string address:text price_paid:decimal email_address:string
to add a row:
ruby script/generate migration AddPhoneToTickets phone:string
important bit is Add…To…
then
rake db:migrate
then changed the 4 view to display the new field.
Starting From Scratch
The scaffolding can create too much code! Here is an app just generating 1 page to start with (a show page)
ruby script/generate model ad name:string description:text price:decimal seller_id:integer email:string img_url:string
models have singular names eg ticket, ad
Controller
ruby script/generate controller ads
controllers have plural names
Routes
map.connect '/ads/:id', :controller=>'ads', :action=>'show'
use the ads_controller.rb, and the show template.
View
show.html.erb
<p>
<b>Name:</b><%= @ad.name %>
</p>
Index Page
controller:
def index
@ads = Ad.find(:all)
end
index.html.erb
<% for ad in @ads%>
<li><a href="/ads/<%= ad.id%>"><%=ad.name%></a></li>
<% end %>
route
map.connect 'ads/', :controller=>'ads', :action=>'index'
Layouts
images into public/images
stylesheet into public/stylesheets
apps/views/layouts/ads.html.erb
Post New Ads Online
routes
map.connect 'ads/new', :controller=>'ads', :action=>'new'
map.connect 'ads/create', :controller=>'ads', :action=>'create'
viewff
ads/new.html.erb
and the form will be submitted to:
ads/create
RoR Text Editor
Am trying http://www.e-texteditor.com/
ctrl shift p – toggle between project view and editor view
http://www.e-texteditor.com/wiki/index.php/Projects#Keyboard_navigation – keyboard shortcuts
Create Form Helper
Rails can create forms that are associated with model objects. so in new.html.erb
<% form_for(@ad,:url=>{:action=>'create'}) do |f| %>
<p><b>Name</b><br /><%= f.text_field :name %></p>
<p><b>Description</b><br /><%= f.text_area :description %></p>
<p><b>Price</b><br /><%= f.text_field :price %></p>
<p><b>Seller</b><br /><%= f.text_field :seller_id %></p>
<p><b>Email</b><br /><%= f.text_field :email %></p>
<p><b>Img url</b><br /><%= f.text_field :img_url %></p>
<p><%= f.submit "Create" %></p>
<% end %>
eg this comes out as:
<b>Name</b><br /><input id="ad_name" name="ad[name]" size="30" type="text" />
so when the submit button is pressed, this routes back to the create action in the AdsController.
Controller
def new
@ad = Ad.new
end
def create
@ad = Ad.new(params[:ad])
@ad.save
redirect_to "/ads/#{@ad.id}"
end
note: create an empty ad object before passing onto new
and we have an automatic redirect to the listing
otherwise could have commented out and it would go automatically to create.html.erb:
Edit a Record
routes add in
map.connect 'ads/:id/edit', :controller=>'ads', :action=>'edit'
map.connect 'ads/:id/update', :controller=>'ads', :action=>'update'
controller add in
def edit
@ad = Ad.find(params[:id])
end
def update
@ad = Ad.find(params[:id])
@ad.update_attributes(params[:ad])
redirect_to"/ads/#{@ad.id}"
end
view add in
<% form_for(@ad,:url=>{:action=>'update'}) do |f| %>
<p><b>Name</b><br /><%= f.text_field :name %></p>
<p><b>Description</b><br /><%= f.text_area :description %></p>
<p><b>Price</b><br /><%= f.text_field :price %></p>
<p><b>Seller</b><br /><%= f.text_field :seller_id %></p>
<p><b>Email</b><br /><%= f.text_field :email %></p>
<p><b>Img url</b><br /><%= f.text_field :img_url %></p>
<p><%= f.submit "Update" %></p>
<% end %>
Security
in the AdsController
before_filter :check_logged_in, :only => [:edit, :update]
private
def check_logged_in
authenticate_or_request_with_http_basic("Ads") do |username, password|
username == "admin" && password == "secret"
end
end

Delete
Just calling a single action on the controller called destory which then redirects back to index.
**TODO put this live