Jump to content

The ultimate community for Ruby on Rails developers.


DRYing up my controllers


  • Please log in to reply
3 replies to this topic

#1 NerdcoreSteve



  • Members
  • 25 posts

Posted 09 June 2014 - 05:32 PM

My two controllers

Look almost identical, what's the best way to DRY this up, or is it best I leave things as they are?

#2 Ohm



  • Moderators
  • 529 posts
  • LocationCopenhagen

Posted 09 June 2014 - 07:54 PM   Best Answer

Those two links are the same, of course they look identical  :P


I think you mean the people and the thing controllers, right? Those look pretty similar, but I wouldn't do any DRYing it up on those two. I believe that you need to be able to read the files individually as well, and the only thing to do here would be to create a third controller, that the two first could inherit from.

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk

#3 NerdcoreSteve



  • Members
  • 25 posts

Posted 09 June 2014 - 11:27 PM

Whoops. :) Yes I meant the person and thing controllers. :)


That advice seems reasonable. Readability aside, it would also mean more work! I have other stuff I want to do to get this thing usable. I might still revisit this at a later date after I've learned more.

#4 ccmcbeck



  • Members
  • 1 posts

Posted 10 February 2015 - 06:56 PM

What if you are using transactions in your controller actions?  When I delete a Person, I want to log an Activity so I wrap both of those in a transaction:

person.transaction do
  user.track person, :destroy

I believe it's a best practice to user the bang! methods in a transaction.  But what if an exception is raised like ActiveRecord::RecordNotDestroyed or in my case a custom Person::DeleteLastAdmin < StandardError exception?


Should the controller use a begin..rescue construct like:

  # do transaction
  # report success for each format
rescue StandardError
  # report failure for each format

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users