diff --git a/app/controllers/rates_controller.rb b/app/controllers/rates_controller.rb index a62fbe3..2b6e953 100644 --- a/app/controllers/rates_controller.rb +++ b/app/controllers/rates_controller.rb @@ -2,6 +2,7 @@ class RatesController < ApplicationController helper :users before_filter :require_admin before_filter :require_user_id, :only => [:index, :new] + before_filter :set_back_url, :only => [:new, :edit] # GET /rates?user_id=1 # GET /rates.xml?user_id=1 @@ -49,7 +50,13 @@ class RatesController < ApplicationController respond_to do |format| if @rate.save flash[:notice] = 'Rate was successfully created.' - format.html { redirect_to(rates_url(:user_id => @rate.user_id)) } + format.html { + if params[:back_url] && !params[:back_url].blank? + redirect_to(params[:back_url]) + else + redirect_to(rates_url(:user_id => @rate.user_id)) + end + } format.xml { render :xml => @rate, :status => :created, :location => @rate } else format.html { render :action => "new" } @@ -100,4 +107,8 @@ class RatesController < ApplicationController end end end + + def set_back_url + @back_url = params[:back_url] + end end diff --git a/app/views/rates/_form.html.erb b/app/views/rates/_form.html.erb index 4501424..5bf5dfe 100644 --- a/app/views/rates/_form.html.erb +++ b/app/views/rates/_form.html.erb @@ -23,6 +23,7 @@ <%= f.hidden_field "user_id" %> + <%= hidden_field_tag "back_url", @back_url %> <%= submit_tag l(:button_add),:class => 'button-small' -%> diff --git a/app/views/users/_rates.html.erb b/app/views/users/_rates.html.erb index 56f9be1..04550f6 100644 --- a/app/views/users/_rates.html.erb +++ b/app/views/users/_rates.html.erb @@ -1,6 +1,7 @@

<%= l(:rate_label_new_rate) %>

<% @rate = Rate.new(:user => @user ) %> +<% @back_url = url_for(:controller => 'users', :action => 'edit', :id => @user, :tab => 'rates') %> <%= render :partial => 'rates/form' %>

<%= l(:rate_label_rate_history) %>

diff --git a/spec/controllers/rates_controller_spec.rb b/spec/controllers/rates_controller_spec.rb index 7e9aa42..8761599 100644 --- a/spec/controllers/rates_controller_spec.rb +++ b/spec/controllers/rates_controller_spec.rb @@ -272,6 +272,13 @@ describe RatesController, "as an administrator" do response.should redirect_to(rates_url(:user_id => @user.id)) end + it 'should redirect to the back_url if set' do + back_url = '/back_to_this_url' + Rate.stub!(:new).and_return(mock_rate(:save => true)) + post :create, :rate => {}, :back_url => back_url + response.should redirect_to(back_url) + end + end describe "with invalid params" do