This is the first time I've run into an issue like this.
I've been using focused_controller in some projects and in others I've just extracted the expose
method (see below). Everything was always fine.
Now in a CRM app I have a CommentsController
that handles comments for multiple commentable resources – e.g. a contact. The comment form is currently always displayed on the resource's show
page. So in case of errors (e.g. empty comment text), the commentable's show
template is the one that needs to be re-rendered in order to properly show error messages. I don't want to do ugly stuff like redirect with error messages in the GET parameters or put something in the session.
As it stands, I found two solutions that actually work and allow me to use contact
(as a method call) in the show
template:
- Use a combination of
instance_variable_set/get/defined?
. Remember: the commentable is essentially polymorphic so I have to set the name dynamically. - Pass a
:locals
hash to therender
call.
I don't really mind the :locals
approach – but at the same time I feel this should be easier – e.g. by using an instance-level expose
call.
So the problem is that you are rendering the
show
view for a resource, e.g.contact
, and understandably using the name of the resource in the view code rather than a generic term.I think
:locals
is the most correct approach in this situation. I probably would have structured my controllers differently to avoid the situation. For example by having a controller for each commentable resource that inherits from a base commentable controller but has its ownexpose
method for the resource. So your route would be/contacts/23/comments/
and aContactCommentsController < CommentsController
. Because I'd prefer to load the resource model directly then build the comment on it rather than the other way around. Also I often need to do more to render the "show" page of a resource than just loading a single model. Can be nicely solved with AJAX too.Sorry probably not very helpful!