FYI: Overriding #edit and avoiding in-place versions of Hobo buttons

22 views
Skip to first unread message

Tim Griffin

unread,
Sep 16, 2011, 4:26:53 PM9/16/11
to hobo...@googlegroups.com
Hi all;

Just passing along some information conveyed to me by Bryan Larsen. 

In my controller, I had overridden an #edit action as follows:

  def edit
     @document = Document.find_by_id(params[:id])
     ...
  end

The trouble with this method of loading the object being edited (the document) is that it's not setting a critical, internal Hobo variable named "this". The consequence of this was that my edit.dryml page's use of the <delete-button> tag was creating an ajaxified "in-place" version of the button instead of the usual button that would take me off the page. I would click the Delete button to delete the record being edited and expected to be redirected to the model's index page (as would be the automatic action after the delete), but after deleting the object, my browser just sat there not knowing what to do next, entertaining me with the "Removing" spinner.

It turns out that Hobo's internal code for the <delete-button> tag includes the following line:

    in_place = false if in_place.nil? && this == @this && request.method.downcase == "get"

In my case, the "this == @this" comparison was failing because there had been no assignment to @this. 

So, my #edit override really needed to make this assignment instead:

    self.this = Document.find_by_id(params[:id]) 

The assignment to self.this also magically looks after setting @document needed by the view.

And, bingo, I get the normal <delete-button> that properly moves me off the page as I'd hoped.

Thanks, Bryan.

Tim

Matt Jones

unread,
Sep 16, 2011, 6:17:42 PM9/16/11
to hobo...@googlegroups.com

On Sep 16, 2011, at 4:26 PM, Tim Griffin wrote:

> Hi all;
>
> Just passing along some information conveyed to me by Bryan Larsen.
>
> In my controller, I had overridden an #edit action as follows:
>
> def edit
> @document = Document.find_by_id(params[:id])
> ...
> end
>
> The trouble with this method of loading the object being edited (the document) is that it's not setting a critical, internal Hobo variable named "this". The consequence of this was that my edit.dryml page's use of the <delete-button> tag was creating an ajaxified "in-place" version of the button instead of the usual button that would take me off the page. I would click the Delete button to delete the record being edited and expected to be redirected to the model's index page (as would be the automatic action after the delete), but after deleting the object, my browser just sat there not knowing what to do next, entertaining me with the "Removing" spinner.
>
> It turns out that Hobo's internal code for the <delete-button> tag includes the following line:
>
> in_place = false if in_place.nil? && this == @this && request.method.downcase == "get"
>
> In my case, the "this == @this" comparison was failing because there had been no assignment to @this.

That's quite peculiar - calling 'this' (a function) should have loaded @this from @document (around line 787 in lib/hobo/controller/model.rb). Was the controller doing this named something out of the ordinary (ie, not 'DocumentsController')?

--Matt Jones

Tim Griffin

unread,
Sep 16, 2011, 6:54:29 PM9/16/11
to hobo...@googlegroups.com
I wish I could say 'yes' but the controller was named DocumentsController and based directly on ApplicationController. Nothing unusual. 

T

Matt Jones

unread,
Sep 16, 2011, 11:05:00 PM9/16/11
to hobo...@googlegroups.com

On Sep 16, 2011, at 6:54 PM, Tim Griffin wrote:

> I wish I could say 'yes' but the controller was named DocumentsController and based directly on ApplicationController. Nothing unusual.
>

Wow. Finally ran this down - the problem came down to the order of some statements in DRYML's call_render function. Everything *looked* OK, but the instance variable that was being set had *already* been copied into the TemplateEnvironment (as nil).

Thanks!

--Matt Jones

Tim Griffin

unread,
Sep 16, 2011, 11:15:16 PM9/16/11
to hobo...@googlegroups.com

Okay, does that mean that (after picking up your patch in Hobo), I should return to my original approach, as a matter of style? I have to admit that thinking in terms of setting "self.this" is a bit low-level for my liking when (really) all I'm thinking of doing is loading a document by id. 

Will your patch correctly set self.this now if do @document = Document.find_by_id(params[:id])?

T

Matt Jones

unread,
Sep 17, 2011, 9:01:10 AM9/17/11
to hobo...@googlegroups.com

On Sep 16, 2011, at 11:15 PM, Tim Griffin wrote:

>
> Okay, does that mean that (after picking up your patch in Hobo), I should return to my original approach, as a matter of style? I have to admit that thinking in terms of setting "self.this" is a bit low-level for my liking when (really) all I'm thinking of doing is loading a document by id.
>
> Will your patch correctly set self.this now if do @document = Document.find_by_id(params[:id])?

Yep - the code was trying really hard to do it before, but the data wasn't making it into the template environment. It should work now.

--Matt Jones

Reply all
Reply to author
Forward
0 new messages