section led by ivan lee reachable at ivan period lee at cs period stanford period edu 1
DESCRIPTION
… 3TRANSCRIPT
Section led by Ivan Lee
Reachable at ivan period lee at cs period stanford period edu
1
<% form_for :ninjaTurtle :url => { :action => “train" } do |f| %>Name: <%= f.text_field :name %><%= f.text_field :color %><%= f.text_area :favToppings %><%= f.text_field :weapon%><%= submit_tag ‘Cowabunga!' %>
<% end %>
2
<form action="/turtles/train" method="post"><div style="margin:0;padding:0"><input id=“ninjaTurtle_name"
name=“ninjaTurtle[name]" size="30" type="text" />
…<input name="commit" type="submit"
value=“Cowabunga!" /></form>
3
In your controller…Access using params[:ninjaTurtle][:name]
Ex:If params[:ninjaTurtle][:name] == ‘turtle’
flash[:warning] = “Your turtle needs a better name!”# see how “flash” is displayed a few slides down
end4
<% form_for(:pics_or_it_didnt_happen, :url => {:action => :photoPost}, :html => { :multipart => true }) do |form| %><%= form.file_field :pic_field %><br /><%= submit_tag "Upload Picture" %>
<% end %>
5
<% if flash[:warning] %><div class="warning">
<%= flash[:warning] %></div>
<% end %>
6
You can treat it as a global variablesession["login_id"] = user.id;If session["login_id"] == nil…
Can store all sorts of fun stuff in the session
But when you’re all done, clean up!reset_session7
Usageif (dont_want_to_reload_page)
render(:controller => turtles, :action => :attack)
end
Note: “:action” renders the view with the same name as the action
8
From the Rails book, pg. 397Many forms require a user to enter some
piece of information twice, the second copy acting as a confirmation that the first was not mistyped. If you use the naming convention that the second field has the name of the attribute with _confirmation appended, you can use validates_confirmation_of to check that the two fields have the same value. The second field need not be stored in the database.
9
Ex (if you really wanted to ensure the Ninja Turtle’s favorite color):
<%= f.text_field:color” %><br /><%= f.text_field:color_confirmation”
%> <br />class Turtle < ActiveRecord::Base
validates_confirmation_of :colorend10
if (there_is_a_post_error)@errorMessage = “You can’t do that!”render(:action => previousAction)return
end
11
To look something up in the database by multiple fields, use Model.find(:first, :conditions => { … } )
Write out a map of what redirects whereLast part: basically don’t use self.method
Def methodNOT Def MyClass.method or def self.method
Make your layouts prettier – this helps with frustration when debugging“but it still looks so nice!”
12
13