Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Glenn Mc's Profile

GitHub User: agile5

Comments by Glenn Mc

Avatar

confirmed - got my invite today! let's play...

Avatar

Thanks Ryan, great as usual! I wonder if Heroku or Appfog have any issues around using Postgres Schemas e.g. limitations on the dev databases therein? I used scopes 2 years ago when I last did a multi-tenant app but at the time I wanted to make use of Schemas I have to say - it seems less fussy and more secure plus less likely to forget to scope data etc. Maybe I'll migrate the old site to this method (only 3 subdomains including www) if Heroku supports it...

Avatar

Wow, coming up to 3 years old and this is still a super easy way of doing sorting by dragging. I have just used this approach alongside your brilliant nested attributes tutorials (http://railscasts.com/episodes/196-nested-model-form-part-1) and it works just as well as the simple model example you use in this tutorial.

For me I have a Page model with nested attributes on a Snippet model (all very CMS based). If anyone was thinking of doing the same, go for it. I did this - it may help you:

1 - added a controller for the nested model - "Snippet" in my case (not needed otherwise) which just contains the "sort" method as you have it i.e.

ruby
class SnippetsController < ApplicationController

  def sort
    params[:snippets].each_with_index do |id, index|
      Snippet.update_all(["position=?", index+1], ["id=?", id])
    end
    render :nothing => true
  end

end

2 - add the drag n drop code to my show view rather than my index view like this:

erb
<ul id="snippets">
  <% for snippet in @page.snippets %>
    <%= content_tag_for :li, snippet do %>
      <span class="handle">[drag]</span>
      <%= snippet.body %>
    <% end %>
  <% end %>
</ul>
<%= sortable_element("snippets", :url => sort_snippets_path, :handle => "handle") %>

it helped me to keep the sorting feature out of the way of the nested attribute feature in the edit view for my parent model

3 - since i am Rails 3.0.x based i have added a default_order scope to my nested model to handle the sorting bit:

ruby
class Snippet < ActiveRecord::Base
  belongs_to  :page
  acts_as_list
  
  scope :default_order, order("position")
end

4 - for Rails 3 i have also used the routing approach added by Tyler Gannon in these comments i.e.

ruby
resources :snippets do
  collection do
    post :sort
  end
end

all works a treat. thank you for your excellent work as always Ryan!

glenn

p.s. the great thing about all this is that it just works without messing about! i was especially happy that the sort method only acts on those "id's" that i am currently working within my "show" view and NOT all objects i.e. it updates the positions for those snippets in my current page, not all snippets. the acts_as_list gem is optional for me in that my position attribute is optional but i may also end up with several snippets with NO positions by default so i'd sooner have positions added by the acts_as_list gem on new snippets - even if the calculated 'next position' is not scoped to my current page model. it's no big deal because as soon as you reorder your snippets it all sorts itself out!

Avatar

see my post below Hannes H. - you should be OK.

Avatar

wow - monster cast! love your work Ryan and i'm happy to start paying you some money for Pro as you have taught me so much over the years / every day still! i'm UK based and as Jon Hope said, online payment solutions in the UK are in the dark ages and PayPal is a safe route. i don't think a WPP account is necessary though, a Standard Business account should suffice when using the PP Express Checkout approach (source: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_ECGettingStarted) ... and i quote from there:

To use Express Checkout, you must have: A Business or Premier account. A Business or Premier account enables you to become a merchant for whom PayPal collects money from buyers for goods or services. PayPal manages these transactions and charges you a small fee and a percentage of the amount collected from the buyer for each transaction.

I hope that helps clear up some confusion for British (and probably German) customers.