DRY respond_to

Here is how my restful new action look like:

def new
@foo = @widget.build(params[:foo])
raise NoPost unless request.post?
@foo.save!
respond_to do |type|
type.html { redirect_to foo_url }
type.js { render :action => :create }
end
rescue NoPost
respond_to do |type|
type.html { render :action => :new }
type.js { render :partial => ‘form’ }
end
rescue ActiveRecord::RecordInvalid
respond_to do |type|
type.html { render :action => :new }
type.js { render :action => :create }
end
end
alias :create :new

Any ideas how to DRY it?

rescue NoPost, ActiveRecord::RecordInvalid ?

Vishnu G. wrote:

rescue NoPost, ActiveRecord::RecordInvalid ?

But those 2 rescues are different

Ah sorry, didn’t notice. There’s not much to DRY tho in that case. Try
this:

rescue NoPost, ActiveRecord::RecordInvalid
[…]
type.js { render :action => ($!.is_a? NoPost) ? :new : :create }

Just typing this out, but some variation of that will work.

Vish