Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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