RailsCasts Pro episodes are now free!

Learn more or hide this

James Lee's Profile

GitHub User: amesee

Comments by James Lee

Avatar

Or going even further, move all that database stuff into the model.

/app/models/faq.rb
def self.sort_positions!(ids)
  ids.each_with_index do |id, index|
    update_all({ position: index + 1 }, { id: id })
  end
end
/app/controllers/sorts_faqs_controller.rb
class SortsFaqsController < ApplicationController
  def create
    User.sort_positions!(params[:list])
    render nothing: true
  end
end
Avatar

One thing I would do different is move the sorting into a new controller class and put the sorting in its #create action. This way we can stay within the boundaries of the seven actions.

/app/controllers/sorts_faqs_controller.rb
class SortsFaqsController < ApplicationController                               
  def create                                                                    
    # same as FaqsController#sort shown in screencast                                                        
  end                                                                           
end
config/routes.rb
resource :sorts_faqs, only: :create                                             
app/views/faqs/index.html.erb
<ul id="faqs" data-update-url="<%= sorts_faqs_path %>">
<% @faqs.each do |faq| %>
  <!-- same as shown in screencast -->
<% end %>
</ul>