Skip to content

Commit

Permalink
fancier cancel_link form helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Carpenter committed Mar 17, 2010
1 parent 7aa86d3 commit 02826aa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
13 changes: 12 additions & 1 deletion app/controllers/application_controller.rb
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/views/users/_form.html.erb
Expand Up @@ -5,4 +5,9 @@
<% if permitted_to?(:set_role, @user) %>
<%= f.input :role, :as => :select, :collection => User::ROLES %>
<% end %>
<% end %>
<% end %>
<% f.buttons do %>
<%= f.commit_button %>
<%= f.cancel_link %>
<% end %>
4 changes: 1 addition & 3 deletions app/views/users/edit.html.erb
Expand Up @@ -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 %>
3 changes: 0 additions & 3 deletions 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 %>
13 changes: 11 additions & 2 deletions 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)
Expand Down

0 comments on commit 02826aa

Please sign in to comment.