Responding to actions differently based on caller/context?

2 views
Skip to first unread message

James K

unread,
Jul 16, 2008, 5:34:05 PM7/16/08
to Boston Ruby Group
I'm working on an application in which one can edit objects both from
a summary of the object in a collection of the objects and also from a
details page for the edit. In both cases, the edit of the associated
model happens in a form rendered pop-up like in a div.

The rub is that I want the update action to respond differently based
on the page that it is called from. If called from the collection view
I'd like to render an updated row, but if called from an object's show
detail screen it should cause the page to be refreshed.

One approach being considered is to stick a parameter in the form so
the action can switch its response based on that, but I'm hoping for a
more elegant solution.

This seems like a problem any non-trivial RESTful application would
have but I haven't stumbled over any nice solutions, or I could be
just over thinking the whole thing.

Does anyone have any experience with this type of situation or
suggestions?

thanks

-James

Steve Harris

unread,
Jul 16, 2008, 7:06:33 PM7/16/08
to boston-r...@googlegroups.com
Not sure if this qualifies as RESTful, but what about something like
the "store_location" and "redirect_back_or_default" methods in the
acts_as_authenticated plugin?

- Steve

Neil Cook

unread,
Jul 16, 2008, 7:22:24 PM7/16/08
to boston-r...@googlegroups.com
Assuming that we are talking about Rails...

Since you want one version to update a row in a list while you want to
refresh the whole page in the other case, it seems that you want to
return RJS in the former and HTML in the latter.

You are performing an edit action in both cases. In the first case you
can detect an XMLHttpRequest and respond with RJS. In the second you can
re-direct to the show detail action for the object.

In Rails use remote_form_for and remote_form. Implment the DIV in a
partial and chose the method depending upon a parameter passed to the
partial.

/Neil

Matthew Krom

unread,
Jul 16, 2008, 8:09:35 PM7/16/08
to boston-r...@googlegroups.com, boston-r...@googlegroups.com
You can also use "if request.xhr?" which is true for Ajax requests.

matthe...@gmail.com
mobile 617 852 5130

James K

unread,
Jul 16, 2008, 11:16:47 PM7/16/08
to Boston Ruby Group
Yes to rails.
How about if in both instances it is an AJAX call, and to a different
controller (the edit is on a nested/child model)?


On Jul 16, 7:22 pm, Neil Cook <n.k.c...@gmail.com> wrote:
> Assuming that we are talking about Rails...
>
> Since you want one version to update a row in a list while you want to
> refresh the whole page in the other case, it seems that you want to
> return RJS in the former and HTML in the latter.
>
> You are performing an edit action in both cases. In the first case you
> can detect an XMLHttpRequest and respond with RJS. In the second you can
> re-direct to the show detail action for the object.
>
> In Rails use remote_form_for and remote_form. Implment the DIV in a
> partial and chose the method depending upon a parameter passed to the
> partial.
>
> /Neil
>
> Steve Harris wrote:
> > Not sure if this qualifies as RESTful, but what about something like
> > the "store_location" and "redirect_back_or_default" methods in the
> > acts_as_authenticated plugin?
>
> > - Steve
>

Matthew Krom

unread,
Jul 17, 2008, 8:38:55 AM7/17/08
to boston-r...@googlegroups.com
You can create two different actions (with different renders/redirects), and have them both use the same "helper" method that contains the common logic.

Matt
Reply all
Reply to author
Forward
0 new messages