|
| 1 | +# a collection of default responses to reduce clutter in the controllers |
1 | 2 | module DefaultResponses |
2 | 3 | extend ActiveSupport::Concern |
3 | 4 |
|
4 | | - def default_save_response(model) |
| 5 | + def redirect_or_render_on_action(model, success_message, error_view) |
5 | 6 | respond_to do |format| |
6 | | - if model.save |
7 | | - format.html { redirect_to model, notice: '%s was successfully created.' % model.class } |
| 7 | + if yield |
| 8 | + format.html { redirect_to model, notice: success_message } |
8 | 9 | format.json { render :show, status: :created, location: model } |
9 | 10 | else |
10 | | - format.html { render :new } |
| 11 | + format.html { render error_view } |
11 | 12 | format.json { render json: model.errors, status: :unprocessable_entity } |
12 | 13 | end |
13 | 14 | end |
14 | 15 | end |
15 | 16 |
|
| 17 | + def default_save_response(model) |
| 18 | + msg = format('%s was successfully created.', model.class) |
| 19 | + redirect_or_render_on_action(model, msg, :new) { model.save } |
| 20 | + end |
| 21 | + |
16 | 22 | def default_update_response(model, params) |
17 | | - respond_to do |format| |
18 | | - if model.update(params) |
19 | | - format.html { redirect_to model, notice: '%s was successfully updated.' % model.class } |
20 | | - format.json { render :show, status: :ok, location: model } |
21 | | - else |
22 | | - format.html { render :edit } |
23 | | - format.json { render json: model.errors, status: :unprocessable_entity } |
24 | | - end |
25 | | - end |
| 23 | + msg = format('%s was successfully updated.', model.class) |
| 24 | + redirect_or_render_on_action(model, msg, :edit) { model.update(params) } |
26 | 25 | end |
27 | 26 |
|
28 | 27 | def default_destroy_response(destination_url, success_msg) |
|
0 commit comments