Created
August 18, 2009 18:47
-
-
Save bigeasy/169877 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%form{:action => thing_url(@thing), :method => 'post'} | |
%input{:type => 'hidden', :name => '_method', :value => 'delete'} | |
%input{:type => 'hidden', :name => 'authenticity_token', :value => form_authenticity_token} | |
%input{:type => 'submit', :value => 'Delete'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%input{:type => 'hidden', :name => 'authenticity_token', :value => form_authenticity_token} | |
%label= "Name:" | |
%input{:name => 'thing[name]', :value => @thing.name} | |
%label= "Size:" | |
%input{:name => 'thing[size]', :value => @thing.size} | |
%label= "Description:" | |
%textarea{:name => 'thing[description]'}= @thing.description | |
%input{:type => 'submit', :value => 'Save'} | |
- if @thing.errors.any? | |
%ul | |
- @thing.errors.each_full do |msg| | |
%li= msg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1= "Editing %s" % @thing.name | |
%form{:action => thing_url(@thing), :method => 'post'} | |
%input{:type => 'hidden', :name => '_method', :value => 'put'} | |
= self.render('form_fields') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1= "All things" | |
%ul | |
- @things.each do |thing| | |
%li | |
%a{:href => thing_url(thing)}= thing.name | |
%a{:href => new_thing_url}= "Make a new thing" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1= "Making a new thing" | |
%form{:action => things_url, :method => 'post'} | |
= self.render('form_fields') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActionController::Routing::Routes.draw do |map| | |
map.things( '/things', { :controller => 'things', :action => 'index', :conditions => { :method => :get } }) | |
map.things( '/things', { :controller => 'things', :action => 'create', :conditions => { :method => :post } }) | |
map.new_thing( '/things/new', { :controller => 'things', :action => 'new', :conditions => { :method => :get } }) | |
map.edit_thing('/things/:id/edit', { :controller => 'things', :action => 'edit', :conditions => { :method => :get } }) | |
map.thing( '/things/:id', { :controller => 'things', :action => 'show', :conditions => { :method => :get } }) | |
map.thing( '/things/:id', { :controller => 'things', :action => 'update', :conditions => { :method => :put } }) | |
map.thing( '/things/:id', { :controller => 'things', :action => 'destroy', :conditions => { :method => :delete } }) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1= @thing.name | |
%div | |
%a{:href => edit_thing_url(@thing)}= "edit" | |
= self.render('delete') | |
%a{:href => things_url}= "Go back" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Thing < ActiveRecord::Base | |
self.validates_presence_of(:name, :size, :description) | |
self.validates_uniqueness_of(:name) | |
self.validates_numericality_of(:size) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!!! Strict | |
%html | |
%head | |
%body | |
= yield |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ThingsController < ApplicationController | |
# call-seq: | |
# GET http://domain.tld/things | |
# | |
def index | |
@things = Thing.all | |
end | |
# call-seq: | |
# GET http://domain.tld/things/:id | |
# | |
def show | |
@thing = Thing.find(params[:id]) | |
end | |
# call-seq: | |
# GET http://domain.tld/things/new | |
# | |
def new | |
@thing = Thing.new | |
end | |
# call-seq: | |
# POST http://domain.tld/things | |
# | |
def create | |
@thing = Thing.new(params[:thing]) | |
@thing.save! | |
self.redirect_to(things_url) | |
rescue ActiveRecord::RecordInvalid | |
self.render('new') | |
end | |
# call-seq: | |
# GET http://domain.tld/things/:id/edit | |
# | |
def edit | |
@thing = Thing.find(params[:id]) | |
end | |
# call-seq: | |
# PUT http://domain.tld/things/:id | |
# | |
def update | |
@thing = Thing.find(params[:id]) | |
@thing.update_attributes!(params[:thing]) | |
self.redirect_to(thing_url(@thing)) | |
rescue ActiveRecord::RecordInvalid | |
self.render('edit') | |
end | |
# call-seq: | |
# DELETE http://domain.tld/things/:id | |
# | |
def destroy | |
Thing.delete(params[:id]) | |
self.redirect_to("/things") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment