handling errors when separating controllers

Skip to first unread message


Mar 24, 2020, 1:32:51 PM3/24/20
to Ruby on Rails: Talk
A while ago I listened to an interview with DHH where he outlined the advantages of using new controllers rather than creating custom actions in existing controllers.

I have been trying this approach, and like the way it improves organisation and simplifies routes and also encourages scoping/namespacing to group resources.

The one thing I have found not too neat is the handling of errors between resources.  In particular, if I need to use partials from the original resouce (eg to resend the show action), the pathname for the partials are wrong since they try to prefix with the alternative controller name.  This can be overcome by using absolute paths for partial names.  But this tends towards cluttered and brittle code.

Mostly I get over this by using ajax which allows me to render nothng inline and just re-display the partial that displays error messages.   However, I have a products template that includes a form for uploading images.  I was doing a rewrite in this area and tried moving the create_image method out of products controller and into an images controller/resource.

But since the multipart form cannot be remoted, I am stuck with an html request.  To display an error message from this action now forces me to either redirect (which will reset the page to its initial show state) or use absolute pathnames for the partials in the products#show so I can call products#show from images#create.  (there are quite a few partials involved)

It occurred to me that this may have been addressed, or that there may be a workaround to override the default partial pathname. 

I have tried googling and not come up with very much that helps - I cant really think of a succinct phrase to google.

Does anyone have any ideas or suggestions (or am I missing something).


Reply all
Reply to author
0 new messages