From a7b6b69ce9f309e3e8616b3ad2c8f561590f5170 Mon Sep 17 00:00:00 2001 From: E-Max Date: Sun, 1 Jul 2012 20:32:04 +0300 Subject: [PATCH] Halting page with 404, if record is not found, by default --- .../templates/page/controller.rb.tt | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/padrino-admin/lib/padrino-admin/generators/templates/page/controller.rb.tt b/padrino-admin/lib/padrino-admin/generators/templates/page/controller.rb.tt index 6093adc14..77eb91013 100644 --- a/padrino-admin/lib/padrino-admin/generators/templates/page/controller.rb.tt +++ b/padrino-admin/lib/padrino-admin/generators/templates/page/controller.rb.tt @@ -22,26 +22,38 @@ Admin.controllers :<%= @orm.name_plural %> do get :edit, :with => :id do @<%= @orm.name_singular %> = <%= @orm.find("params[:id]") %> - render '<%= @orm.name_plural %>/edit' + if @<%= @orm.name_singular %> + render '<%= @orm.name_plural %>/edit' + else + halt 404 + end end put :update, :with => :id do @<%= @orm.name_singular %> = <%= @orm.find("params[:id]") %> - if <%= @orm.update_attributes("params[:#{@orm.name_singular}]") %> - flash[:notice] = '<%= @orm.klass_name %> was successfully updated.' - redirect url(:<%= @orm.name_plural %>, :edit, :id => @<%= @orm.name_singular %>.id) + if @<%= @orm.name_singular %> + if <%= @orm.update_attributes("params[:#{@orm.name_singular}]") %> + flash[:notice] = '<%= @orm.klass_name %> was successfully updated.' + redirect url(:<%= @orm.name_plural %>, :edit, :id => @<%= @orm.name_singular %>.id) + else + render '<%= @orm.name_plural %>/edit' + end else - render '<%= @orm.name_plural %>/edit' + halt 404 end end delete :destroy, :with => :id do <%= @orm.name_singular %> = <%= @orm.find("params[:id]") %> - if <%= @orm.destroy %> - flash[:notice] = '<%= @orm.klass_name %> was successfully destroyed.' + if <%= @orm.name_singular %> + if <%= @orm.destroy %> + flash[:notice] = '<%= @orm.klass_name %> was successfully destroyed.' + else + flash[:error] = 'Unable to destroy <%= @orm.klass_name %>!' + end + redirect url(:<%= @orm.name_plural %>, :index) else - flash[:error] = 'Unable to destroy <%= @orm.klass_name %>!' + halt 404 end - redirect url(:<%= @orm.name_plural %>, :index) end end