avatar

42

factor out redirecting; make sure to send a 422 when validations fail, even for html

by chrisk, 24 Jun, 2007 12:49 PM
26 42  
4848     respond_to do |format|
4949       if @exercise.save
5050         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) }
5852         format.xml  { render :xml => @exercise, :status => :created, :location => exercise_path(@exercise.activity, @exercise) }
5953       else
60         format.html { render :action => "new" }
54         format.html { render :action => "new", :status => :unprocessable_entity }
6155         format.xml  { render :xml => @exercise.errors, :status => :unprocessable_entity }
6256       end
6357     end
------
7165     respond_to do |format|
7266       if @exercise.update_attributes(params[:exercise])
7367         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) }
7569         format.xml  { head :ok }
7670       else
77         format.html { render :action => "edit" }
71         format.html { render :action => "edit", :status => :unprocessable_entity }
7872         format.xml  { render :xml => @exercise.errors, :status => :unprocessable_entity }
7973       end
8074     end
------
8781     @exercise.destroy
8882 
8983     respond_to do |format|
90       format.html { redirect_to(exercises_url) }
84       format.html { redirect_to_todays_workout_or_activity(@exercise) }
9185       format.xml  { head :ok }
9286     end
9387   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   
9499 end