public
Description: Controller abstractor for Rails
Homepage: http://mr.hamptoncatlin.com
Clone URL: git://github.com/hcatlin/make_resourceful.git
commit  ae396b8cca7654ea85c36f0001f2ab8931c31c8f
tree    6bef433746f6770a447e9efac0a833283b8b3438
parent  554657cd80ce94805c325101bbb4edb07f5aa4fc
make_resourceful / DEFAULTS
100644 149 lines (119 sloc) 3.884 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
There's a rough equivalence between a basic @make_resourceful@ call
and a hand-made controller.
This:
 
  class PostsController < ApplicationController
    make_resourceful { actions :all }
  end
 
Creates a controller that works more or less like the one that follows.
Note that the real code generated by make_resourceful
is more extensible in various ways.
Thus whenever possible, there are comments in the following controller
indicating how to customize various bits of the controller.
 
  class PostsController < ApplicationController
    def index
      # Override #current_objects to change this
      @posts = Post.find(:all)
 
      # Use before :index to add something here
 
      # Use response_for :index to change this
      respond_to { |f| f.html; f.js }
    end
 
    def show
      # Override #current_object to change this
      @post = Post.find(params[:id])
 
      # Use before :show to add something here
 
      # Use response_for :show to change this
      respond_to { |f| f.html; f.js }
    end
 
    def create
      # Override #build_object to change this
      @post = Post.new(params[:post])
 
      # Use before :create to add something here
 
      if @post.save
        # Use after :create to add something here
 
        # Use response_for :create to change this
        respond_to do |f|
          f.html do
            flash[:notice] = "Create successful!"
            redirect_to post_path(@post)
          end
          f.js
        end
      else
        # Use after :create_fails to add something here
 
        # Use response_for :create_fails to change this
        respond_to do |f|
          format.html
            flash[:error] = "There was a problem!"
            render :action => :new, :status => 422
          end
          format.js
        end
      end
    end
 
    def update
      # Override #current_object to change this
      @post = Post.find(params[:id])
 
      # Use before :update to do something here
 
      if @post.update_attributes params[:post]
        # Use after :update to add something here
 
        # Use response_for :update to change this
        respond_to do |f|
          f.html do
            flash[:notice] = "Save successful!"
            redirect_to post_path(@post)
          end
          f.js
        end
      else
        # Use after :update_fails to add something here
 
        # Use response_for :update_fails to change this
        respond_to do |f|
          format.html
            flash[:error] = "There was a problem saving!"
            render :action => :edit, :status => 422
          end
          format.js
        end
      end
    end
 
    def new
      # Override #build_object to change this
      @post = Post.new(params[:post])
 
      # Use before :new to add something here
 
      # Use response_for :new to change this
      respond_to { |f| f.html; f.js }
    end
 
    def edit
      # Override #current_object to change this
      @post = Post.find(params[:id])
 
      # Use before :edit to add something here
 
      # Use response_for :edit to change this
      respond_to { |f| f.html; f.js }
    end
 
    def destroy
      # Override #current_object to change this
      @post = Post.find(params[:id])
 
      # Use before :destroy to do something here
 
      if @post.destroy
        # Use after :destroy to add something here
 
        # Use response_for :destroy to change this
        respond_to do |f|
          f.html do
            flash[:notice] = "Record deleted!"
            redirect_to posts_path(@post)
          end
          f.js
        end
      else
        # Use after :destroy_fails to add something here
 
        # Use response_for :destroy_fails to change this
        respond_to do |f|
          format.html
            flash[:error] = "There was a problem deleting!"
            render :back
          end
          format.js
        end
      end
    end
  end