factor out redirecting; make sure to send a 422 when validations fail, even for html
| 26 | 42 | |
|---|---|---|
| 48 | 48 | respond_to do |format| |
| 49 | 49 | if @exercise.save |
| 50 | 50 | flash[:notice] = 'Exercise was successfully created.' |
| 51 | format.html { | |
| 52 | if @exercise.created_at.to_date == Time.now.to_date | |
| 53 | redirect_to :controller => 'workouts', :action => 'today' | |
| 54 | else | |
| 55 | redirect_to activity_path(@exercise.activity) | |
| 56 | end | |
| 57 | } | |
| 51 | format.html { redirect_to_todays_workout_or_activity(@exercise) } | |
| 58 | 52 | format.xml { render :xml => @exercise, :status => :created, :location => exercise_path(@exercise.activity, @exercise) } |
| 59 | 53 | else |
| 60 | format.html { render :action => "new" } | |
| 54 | format.html { render :action => "new", :status => :unprocessable_entity } | |
| 61 | 55 | format.xml { render :xml => @exercise.errors, :status => :unprocessable_entity } |
| 62 | 56 | end |
| 63 | 57 | end |
| --- | --- | |
| 71 | 65 | respond_to do |format| |
| 72 | 66 | if @exercise.update_attributes(params[:exercise]) |
| 73 | 67 | flash[:notice] = 'Exercise was successfully updated.' |
| 74 | format.html { redirect_to activity_path(@exercise.activity) } | |
| 68 | format.html { redirect_to_todays_workout_or_activity(@exercise) } | |
| 75 | 69 | format.xml { head :ok } |
| 76 | 70 | else |
| 77 | format.html { render :action => "edit" } | |
| 71 | format.html { render :action => "edit", :status => :unprocessable_entity } | |
| 78 | 72 | format.xml { render :xml => @exercise.errors, :status => :unprocessable_entity } |
| 79 | 73 | end |
| 80 | 74 | end |
| --- | --- | |
| 87 | 81 | @exercise.destroy |
| 88 | 82 | |
| 89 | 83 | respond_to do |format| |
| 90 | format.html { redirect_to(exercises_url) } | |
| 84 | format.html { redirect_to_todays_workout_or_activity(@exercise) } | |
| 91 | 85 | format.xml { head :ok } |
| 92 | 86 | end |
| 93 | 87 | end |
| 88 | ||
| 89 | protected | |
| 90 | ||
| 91 | def redirect_to_todays_workout_or_activity(exercise) | |
| 92 | if exercise.created_at.to_date == Date.today | |
| 93 | redirect_to :controller => 'workouts', :action => 'today' | |
| 94 | else | |
| 95 | redirect_to activity_path(exercise.activity) | |
| 96 | end | |
| 97 | end | |
| 98 | ||
| 94 | 99 | end |
