Skip to content

Instantly share code, notes, and snippets.

@kitop
Created May 7, 2013 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitop/5535042 to your computer and use it in GitHub Desktop.
Save kitop/5535042 to your computer and use it in GitHub Desktop.
class ProductsController < ApplicationController
def preview
begin
ActiveRecord::Base.transaction do
if request.referer =~ %r{/admin/products/(.+)/edit}
@product = Product.find($1)
@product.update_attributes(params[:product])
else
@product = Product.new(params[:product])
raise "Invalid" unless @product.valid?
@product.save
end
response.body = render :layout => 'application'
response.status = 200
@performed_render = true # rails internals
raise 'Changes not saved!'
end
rescue => exception
unless @performed_render
# Product doesn't exist => Invalid product id
# Product is valid => Something else should've gone wrong
if @product.nil? or @product.valid?
raise exception
else
render :text => "Invalid deal:\n #{@product.errors.full_messages.join("\n")}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment