handling errors when separating controllers

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).

Tonypm

There’s a couple of different angles to this. More inline below:

On Mar 24, 2020, at 1:32 PM, tonypm <tonypmartin@hotmail.com> wrote:

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.

You could put up with this, using the fully-qualified path for the render inside the “else” half of your #create and #update methods, since that’s the only place where this issue is going to rear its head.

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)

There’s a jQuery plugin called remoteipart that will allow you to do “Ajax” file uploads. I have used it in Rails projects with a gem, and nothing about your form needs to change to allow it to be remote. But an even better approach would be to use something like Uppy for just the file part of your form, sending your files to the ultimate storage (S3 or whatever) directly, without waiting for the rest of the form to submit. This gets a little more complex at the outset, since you no longer have a single form to handle in one go, but the benefit to your visitors is that the whole form experience is quite a lot smoother, particularly if there are large files 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 want to believe this is possible, perhaps in an around action that you apply to individual controller methods.

Hope this helps,

Walter