Created
May 7, 2013 18:42
-
-
Save kitop/5535042 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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