Re: [Hobo Users] draggable reordering doesnt render using acts_as_list with many-to-many using has_many :through

70 views
Skip to first unread message

Bryan Larsen

unread,
Dec 12, 2012, 3:12:48 PM12/12/12
to hobo...@googlegroups.com
It seems to me that if you used <sortable-collection> on a set of
alteration_assignments rather than on a set of alterations, it should
work. Are you using Hobo 2.0 or 1.3?

Bryan


On Wed, Dec 12, 2012 at 2:45 PM, Ryan <rgr...@gmail.com> wrote:
> I have a many-to-many where the order matters on one side, and the draggable
> reordering that the Agility tutorial uses would be great to use - however it
> doesn't seem to be as easy as the one-to-many example in the tutorial.
>
> I have my objects and relationships set up as:
>
> class Destination < ActiveRecord::Base
>
> hobo_model # Don't put anything above this
>
> fields do
> name :string
> timestamps
> end
> attr_accessible :name, :alterations
>
> has_many :alteration_assignments, :dependent => :destroy
> has_many :alterations, :through => :alteration_assignments, :accessible =>
> true, :dependent => :destroy, :order => "position"
>
> children :alterations
>
>
> class Alteration < ActiveRecord::Base
>
> hobo_model # Don't put anything above this
>
> fields do
> name :string
> script :string
> timestamps
> end
> attr_accessible :name, :script
>
> has_many :alteration_assignments, :dependent => :destroy
> has_many :destinations, :through => :alteration_assignments
>
>
> class AlterationAssignment < ActiveRecord::Base
>
> hobo_model # Don't put anything above this
>
> fields do
> timestamps
> end
> attr_accessible
> belongs_to :destination
> belongs_to :alteration
> acts_as_list :scope => :destination
>
>
> It works great - alterations are added, and the position field is set
> properly when adding an alteration, except the draggable list doesn't render
> - the normal alteration cards show up under show destination, and the normal
> alteration select/add/remove shows up under edit destination.
>
> I'm assuming the problem has to do with the acts_as_list only being on the
> assignment object and not the actual alteration object:
>
> irb(main):004:0> destination.alterations.first.move_to_bottom
> Alteration Load (19.1ms) SELECT "alterations".* FROM "alterations" INNER
> JOIN "alteration_assignments" ON "alterations"."id" =
> "alteration_assignments"."alteration_id" WHERE
> "alteration_assignments"."destination_id" = 4 ORDER BY position LIMIT 1
> NoMethodError: undefined method `move_to_bottom' for #<Alteration:0xaa229e8>
>
>
> irb(main):006:0> destination.alteration_assignments.first.move_to_bottom
> AlterationAssignment Load (1.0ms) SELECT "alteration_assignments".* FROM
> "alteration_assignments" WHERE "alteration_assignments"."destination_id" = 4
> LIMIT 1
> (0.6ms) BEGIN
> SQL (0.9ms) UPDATE "alteration_assignments" SET position = (position - 1)
> WHERE ("alteration_assignments"."destination_id" = 4 AND position > 1)
> AlterationAssignment Load (1.1ms) SELECT "alteration_assignments".* FROM
> "alteration_assignments" WHERE ("alteration_assignments"."destination_id" =
> 4 AND id != 4) ORDER BY alteration_assignments.position DESC LIMIT 1
> (0.8ms) UPDATE "alteration_assignments" SET "position" = 2, "updated_at"
> = '2012-12-12 19:36:58.007222' WHERE "alteration_assignments"."id" = 4
> (0.9ms) SELECT COUNT(*) FROM "alteration_assignments" WHERE
> ("alteration_assignments"."destination_id" = 4 AND position = 2)
> (55.0ms) COMMIT
> => true
>
>
> Is there a way to get Hobo to deal with this automagically like with
> one-to-many, or do I need to write something myself?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/rBbimvP3HzcJ.
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to
> hobousers+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/hobousers?hl=en.

Ryan

unread,
Dec 12, 2012, 4:27:40 PM12/12/12
to hobo...@googlegroups.com

On Wednesday, December 12, 2012 2:12:48 PM UTC-6, Bryan Larsen wrote:
It seems to me that if you used <sortable-collection> on a set of
alteration_assignments rather than on a set of alterations, it should
work.   Are you using Hobo 2.0 or 1.3?

Bryan


It's Hobo 2.0.   I tried replacing the destination edit form and adding <sortable-collection:alteration_assignments/>.  From the cookbook entry on sortable-collection it looked like the model being used needed a controller, so I generated a controller for alteration_assignments.   However the destination edit page kicks out and "undefined method `reorder_alteration_assignments_url' error.    Maybe I am not using the tag correctly, I didn't see any examples and am new to ruby/rails/hobo.

I put this in application.dryml:

<def tag="form" for="Destination">
  <form merge param="default">
    <error-messages param/>
    <field-list fields="name, alterations" param/>
    <sortable-collection:alteration_assignments param/>
    <div param="actions">
      <submit label="#{ht 'destination.actions.save', :default=>['Save']}" param/><or-cancel param="cancel"/>
    </div>
  </form>
</def>

Bryan Larsen

unread,
Dec 12, 2012, 4:31:48 PM12/12/12
to hobo...@googlegroups.com
Do you have auto_actions :all on your new controller? That should
add the reorder action if your model has the position_column method
defined (acts_as_list should do that).

sortable-collection is really designed to be used outside of a form,
since it acts like an editor: aka changes take immediate effect rather
than waiting until a user hits "save".

Bryan
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/ZLz-EuLiPjAJ.

Ryan

unread,
Dec 12, 2012, 5:31:10 PM12/12/12
to hobo...@googlegroups.com
The controller was generated with the hobo generator and does have auto_actions :all in it, and the alteration_assignment object has acts_as_list.

class AlterationAssignmentController < ApplicationController

  hobo_model_controller

  auto_actions :all

end

Bryan Larsen

unread,
Dec 12, 2012, 5:33:54 PM12/12/12
to hobo...@googlegroups.com
what does `rake routes` tell you: are there any reorder actions in there?

Bryan

Ryan

unread,
Dec 12, 2012, 5:36:48 PM12/12/12
to hobo...@googlegroups.com
There is:

reorder_alteration_assignment_index POST   /alteration_assignment/reorder(.:format)               alteration_assignment#reorder

Bryan Larsen

unread,
Dec 12, 2012, 5:42:06 PM12/12/12
to hobo...@googlegroups.com
OK, looks like a bug in the new routing code. I thought I had a
sortable-collection in the integration tests that should have caught
that.

hobo's asking for reorder_alteration_assignments_url, but you have
reorder_alteration_assignment_index_url

I'll take a look tomorrow.

Bryan
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/0e_pWPmxXZIJ.

Ryan

unread,
Dec 12, 2012, 5:45:44 PM12/12/12
to hobo...@googlegroups.com
I've got plenty of other things to work on in the mean time, thanks for your help.

owendall

unread,
Dec 12, 2012, 6:22:23 PM12/12/12
to hobo...@googlegroups.com
Thanks, Bryan.

Sent from my iPhone

Bryan Larsen

unread,
Dec 13, 2012, 11:21:57 AM12/13/12
to hobo...@googlegroups.com
I did have a sortable-collection test in the integration tests, but
when there was a problem that it didn't catch. So please try the
version of hobo from github.

IOW, use

gem 'hobo', :git => 'git://github.com/Hobo/hobo.git'

in your Gemfile. (other hobo gems will probably need the same treatment).

Bryan

Ryan

unread,
Dec 13, 2012, 1:20:59 PM12/13/12
to hobo...@googlegroups.com
I changed my Gemfile to use:

gem 'hobo', :git => 'git://github.com/Hobo/hobo.git'
gem "will_paginate", :git => "git://github.com/Hobo/will_paginate.git"
gem "hobo_jquery_ui"
gem "hobo_bootstrap", :git => "git://github.com/Hobo/hobo_bootstrap.git"

I didn't see a git repository for hobo_jquery_ui so I took out the version assuming it would use the latest.

Then did a bundle install, and it pulled hobo from git.  But I was still getting the same undefined method `reorder_alteration_assignments_url` error.

In hobo_routes.rb I found:
  # Resource routes for controller alteration_assignment
  resources :alteration_assignment do
    collection do
      post 'reorder'
    end
  end

I copied and pasted that route into routes.rb and changed it to the plural :alteration_assignments, the correct route appeared in rake routes, and the sortable-collection started working.

     reorder_alteration_assignments POST   /alteration_assignments/reorder(.:format)              alteration_assignments#reorder
reorder_alteration_assignment_index POST   /alteration_assignment/reorder(.:format)               alteration_assignment#reorder


Anything else I should check?  It seems like there is still a bug, tho specifying the route fixed it for me.

Thanks
Ryan

Bryan Larsen

unread,
Dec 13, 2012, 1:27:36 PM12/13/12
to hobo...@googlegroups.com
There's obviously something that's different in your application than
the test case that I'm using. Would it be possible for you to create
a small demo application that exhibits the problem that I can play
with?

thanks,
Bryan
> https://groups.google.com/d/msg/hobousers/-/G7RwKioVfuUJ.

Ryan

unread,
Dec 13, 2012, 2:45:08 PM12/13/12
to hobo...@googlegroups.com
Try http://github.com/rgrice/sortable_problem.git

Uncommenting the bottom of routes.db makes it work

Thanks
Ryan

Bryan Larsen

unread,
Dec 13, 2012, 2:47:33 PM12/13/12
to hobo...@googlegroups.com
cool, that should make it much easier to track down. It might be
early next week before I get to it, is that OK?

Bryan
> https://groups.google.com/d/msg/hobousers/-/pOHePt0C3DwJ.

Ryan

unread,
Dec 13, 2012, 2:51:31 PM12/13/12
to hobo...@googlegroups.com
Yeah no worries, it works fine with the addition to routes.rb so it's easy to get around.

Thanks
Ryan

Matt Jones

unread,
Dec 13, 2012, 2:57:54 PM12/13/12
to hobo...@googlegroups.com

On Dec 13, 2012, at 2:45 PM, Ryan wrote:

> Try http://github.com/rgrice/sortable_problem.git
>
> Uncommenting the bottom of routes.db makes it work
>
> Thanks
> Ryan

Just guessing, but it looks like having a singular-named controller (alteration_assignment_controller.rb) is confusing the route generation code. Can you try renaming that one to be plural?

--Matt Jones

Ryan

unread,
Dec 13, 2012, 3:04:48 PM12/13/12
to hobo...@googlegroups.com
Thanks Matt, that worked great.  Thanks for all the responses Bryan.
Reply all
Reply to author
Forward
0 new messages