+ All Categories
Home > Documents > Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

Date post: 18-Jan-2018
Category:
Upload: harold-norton
View: 224 times
Download: 0 times
Share this document with a friend
Description:
… 3
13
Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1
Transcript
Page 1: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

Section led by Ivan Lee

Reachable at ivan period lee at cs period stanford period edu

1

Page 2: 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

Page 3: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

<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

Page 4: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

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

Page 5: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

<% 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

Page 6: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

<% if flash[:warning] %><div class="warning">

<%= flash[:warning] %></div>

<% end %>

6

Page 7: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

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

Page 8: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

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

Page 9: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

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

Page 10: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

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

Page 11: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

if (there_is_a_post_error)@errorMessage = “You can’t do that!”render(:action => previousAction)return

end

11

Page 12: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

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

Page 13: Section led by Ivan Lee Reachable at ivan period lee at cs period stanford period edu 1.

13


Recommended