From 02826aa4bab8a84885091d6839f69e1d9de3f0f1 Mon Sep 17 00:00:00 2001 From: Andrew Carpenter Date: Wed, 17 Mar 2010 13:03:00 -0700 Subject: [PATCH] fancier cancel_link form helper method --- app/controllers/application_controller.rb | 13 ++++++++++++- app/views/users/_form.html.erb | 7 ++++++- app/views/users/edit.html.erb | 4 +--- app/views/users/new.html.erb | 3 --- lib/application_form_builder.rb | 13 +++++++++++-- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6df64e1..74758ca 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,8 +26,19 @@ def set_stamper User.stamper ||= current_user end + before_filter :cancel_message + protected - + def cancel_message + if params[:_cancel] + flash[:notice] = 'Action cancelled.' + redirect_to url_for(params.except("_cancel")) + false + else + true + end + end + # Define how declarative authentication responds to permission denied. def permission_denied raise Authorization::NotAuthorized diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb index 5ad07f8..e1ccd2c 100644 --- a/app/views/users/_form.html.erb +++ b/app/views/users/_form.html.erb @@ -5,4 +5,9 @@ <% if permitted_to?(:set_role, @user) %> <%= f.input :role, :as => :select, :collection => User::ROLES %> <% end %> -<% end %> \ No newline at end of file +<% end %> + +<% f.buttons do %> + <%= f.commit_button %> + <%= f.cancel_link %> +<% end %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 464c276..4b2e887 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -3,9 +3,7 @@ <% else %> <% title 'Edit User' %> <% end %> + <% semantic_form_for @user, :url => user_path do |f| %> <%= render :partial => "form", :locals => {:f => f} %> - <% f.buttons do %> - <%= link_to "Cancel", :back %><%= f.commit_button %> - <% end %> <% end %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 14df2f7..afdf67b 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,7 +1,4 @@ <% title 'New User' %> <% semantic_form_for @user do |f| %> <%= render :partial => "form", :locals => {:f => f} %> - <% f.buttons do %> - <%= link_to "Cancel", :back %><%= f.commit_button %> - <% end %> <% end %> diff --git a/lib/application_form_builder.rb b/lib/application_form_builder.rb index ad28466..1c74952 100644 --- a/lib/application_form_builder.rb +++ b/lib/application_form_builder.rb @@ -1,8 +1,17 @@ class ApplicationFormBuilder < Formtastic::SemanticFormBuilder # Custom form builder methods go here - def cancel_link(text = 'Cancel') - template.link_to text, :back + def cancel_link(options = {}) + location = options[:url] || template.request.params[:_cancel_url] || template.request.referer || '/' + template.hidden_field_tag(:_cancel_url, location) + + if location =~ /\?/ + location = location + '&_cancel=1' + else + location = location + '?_cancel=1' + end + + template.content_tag(:li, template.link_to('Cancel', location), :class => "cancel" ) end def calendar_input(method, options)