E-mail
here is how we do it from scaffolding:
<%= 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.
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.
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
ruby script/generate controller ads
controllers have plural names
map.connect '/ads/:id', :controller=>'ads', :action=>'show'
use the ads_controller.rb, and the show template.
show.html.erb
<p> <b>Name:</b><%= @ad.name %> </p>
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'
images into public/images
stylesheet into public/stylesheets
apps/views/layouts/ads.html.erb
map.connect 'ads/new', :controller=>'ads', :action=>'new' map.connect 'ads/create', :controller=>'ads', :action=>'create'
ads/new.html.erb
and the form will be submitted to:
ads/create
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
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.
def new @ad = Ad.new end def create @ad = Ad.new(params[:ad]) @ad.save redirect_to "/ads/#{@ad.id}" end
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:
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 %>
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
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
Just calling a single action on the controller called destory which then redirects back to index.
**TODO put this live
Remember Me
a@href@title, strike