Ugh. This is a can of worms.

On Fri, Aug 23, 2013 at 11:22 AM, Ignacio Huerta <[email protected]>wrote:

> > I'm running into a problem with the controller actions, though. This
> > winds up being run in the context of a polymorphic_actions_for setup
> > (:page, in our case - the name collision with the Page model is actually
> > why we haven't updated to 2.0 yet). When I add this action to the
> > controller:
> >
> >   def new
> >     hobo_create(AnimationPane.create(params[:animation_pane])) do
> >       hobo_ajax_response if request.xhr?
> >     end
> >   end
> >
> > ...the AnimationPanes created don't have the association with the parent
> > Page. My intuition is that I actually need to be editing new_for_page
> > (?) but I don't actually understand the built-in controller actions well
> > enough to know how that changes the code inside the action. (I also feel
> > like the resistance I'm feeling in trying to do this is a clue that
> > there's an easier way to do it.)
>
> Sometimes using the default hobo actions like hobo_create is just not
> appropiate. In this case, I see you are creating a record in the "new"
> action, the common use would be to do that in the "create" action. But I
> suppose you are doing that for a reason :)
>

The reason is "that's how the cookbook did it" (this one:
http://cookbook.hobocentral.net/tutorials/33-ajax-filtering-on-a-partially-completed).
But as we'll see below, this now has its own problems.


> I don't know why it's not working, but I would start debugging by  using
> a simple Rails action:
>
> def new
>   logger.info params.inspect
>   @animation_pane = AnimationPane.new(params[:animation_pane])
>   logger.info @animation_pane.inspect
>   @animation_pane.save!
>   hobo_ajax_response if request.xhr?
> end
>
> I suspect that your action is not receiving the "page_id" parameter that
> should create the relationship. The log should show you more information.
>

Here I needed to be working with the 'new_for_page' action instead of
'new'. When I did that, I could see from the params.inspect that the
page_id parameter was coming in, and I was able to use that to create the
association. I wound up with this:

  def new_for_page
    logger.info params.inspect
    @animation_pane = AnimationPane.new(params[:animation_pane])
    @page = Page.find(params[:page_id])
    @animation_pane.page = @page
    logger.info @animation_pane.inspect
    @animation_pane.save!
    hobo_ajax_response if request.xhr?
  end

This now *does* save the association with the page correctly. However - and
I think this is because I'm using 'new' instead of 'create' - what I'm
seeing is that every AJAX request creates a new AnimationPage. So the
sequence of events looks like this:

1) Request to AnimationPanesController#new_for_page creates AnimationPane
with ID #1. This is associated with Page #10.
2) User selects Animation #100 from the select menu in the form for
AnimationPane #1. This triggers an AJAX request.
3) The AJAX request goes back to AnimationPanesController#new_for_page,
which then adds the association to Animation #100 to AnimationPane #1. It
also creates AnimationPane #2, also associated with Page #10, and returns
the form for #2 to the browser.

Consequently, we have (A) created an AnimationPane we don't need, and (B)
apparently (to the end user) ignored the selection they made.

Obviously I need to re-think the mechanics of this whole thing.

pjm

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/hobousers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to