PageRenderTime 25ms CodeModel.GetById 23ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 0ms

/ruby-mode/Ruby on Rails/scaffold/scaffold.restful.basic

http://github.com/ridgetang/snippets
Unknown | 75 lines | 67 code | 8 blank | 0 comment | 0 complexity | 773ec4b41740188950fd8a5d1389e8ce MD5 | raw file
 1#name : generating a RESTful controller
 2#key : restful
 3#group : rails.scaffold
 4#condition : (rails/controller?)
 5# --
 6before_filter :capture_${1:`(singularize-string (rails/cur-res-title))`}
 7$0
 8def index
 9  @${1:$(pluralize-string text)} = ${2:`(decamelize-string (singularize-string (rails/cur-res-title)))`}.all
10  respond_to do |format|
11    format.html # index.html.erb
12    format.xml  { render :xml => @${1:$(pluralize-string text)} }
13  end
14end
15
16def show
17  respond_to do |format|
18    format.html # show.html.erb
19    format.xml  { render :xml => @$1 }
20  end
21end
22
23def new
24  respond_to do |format|
25    format.html # new.html.erb
26    format.xml  { render :xml => @$1 }
27  end
28end
29
30def edit
31end
32
33def create
34  @$1.update_attributes!(params[:$1])
35  flash[:notice] = t('.flash')
36  respond_to do |format|
37    format.html { redirect_to($1_path(@$1)) }
38    format.xml  { render :xml => @$1, :status => :created, :location => $1_url(@$1) }
39  end
40rescue ActiveRecord::RecordInvalid
41  respond_to do |format|
42    format.html { render :action => "new" }
43    format.xml  { render :xml => @$1.errors, :status => :unprocessable_entity }
44  end
45end
46
47def update
48  @$1.update_attributes!(params[:$1])
49  flash[:notice] = t('.flash')
50  respond_to do |format|
51    format.html { redirect_to($1_path(@$1)) }
52    format.xml  { head :ok }
53  end
54rescue ActiveRecord::RecordInvalid
55  respond_to do |format|
56    format.html { render :action => "edit" }
57    format.xml  { render :xml => @$1.errors, :status => :unprocessable_entity }
58  end
59end
60
61def destroy
62  @$1.destroy
63  flash[:notice] = t('.flash')
64  respond_to do |format|
65    format.html { redirect_to(${1:$(pluralize-string text)}_path) }
66    format.xml  { head :ok }
67  end
68end
69
70private
71
72  def capture_$1
73    @$1 = $2.find(params[:id]) if params[:id]
74    @$1 ||= $2.new
75  end