ruby on rails - Sortable Table Row with Nested Resource -
in rails app, timesheet has_many entries , entry belongs_to timesheet.
class timesheet < activerecord::base has_many :entries, order: 'position', dependent: :destroy end class entry < activerecord::base belongs_to :timesheet end i'm following railscast 147 sortable lists (the updated version). in development log notice params hash correctly updates sort order, on reload doesn't save positions correctly. furthermore, request being processed create action instead of custom sort action. here's controller.
class entriescontroller < applicationcontroller before_filter :signed_in_user before_filter :find_timesheet def index @entries = @timesheet.entries.order("position") @entry = @timesheet.entries.build end def create @entry = @timesheet.entries.build(params[:entry]) @entry.position = @timesheet.entries.count + 1 if @entry.save #flash[:notice] = "entry created" #redirect_to timesheet_entries_path respond_to |format| format.html { redirect_to timesheet_entries_path } format.js end else flash[:alert] = "entry not added" render 'new' end end def destroy @entry = @timesheet.entries.find(params[:id]) @entry.destroy respond_to |format| format.html { redirect_to timesheet_entries_path, flash[:notice] = "entry destroyed" } format.js end end def sort params[:entry].each_with_index |id, index| @timesheet.entries.update_all({position: index+1}, {id: id}) end render nothing: true end private def find_timesheet @timesheet = timesheet.find(params[:timesheet_id]) end end and routes.rb file.
sledsheet::application.routes.draw resources :timesheets resources :entries, only: [:index, :create, :destroy] collection { post :sort } end end end the entries.js.coffee
jquery -> $("#entries tbody").sortable( helper: fixhelper update: -> $.post($(this).data('update-url'), $(this).sortable('serialize')) ).disableselection() the output development log
started post "/timesheets/8/entries" 127.0.0.1 @ 2012-06-04 20:14:18 -0400 processing entriescontroller#create */* parameters: {"entry"=>["60", "59", "61"], "timesheet_id"=>"8"} user load (0.2ms) select "users".* "users" "users"."remember_token" = 'qds53hgowfrmbnn9jkau3w' limit 1 timesheet load (0.1ms) select "timesheets".* "timesheets" "timesheets"."id" = ? order date desc limit 1 [["id", "8"]] completed 500 internal server error in 2ms nomethoderror (undefined method `stringify_keys' "60":string): app/controllers/entries_controller.rb:11:in `create' i googled error undefined method, i'm confused why create action called in case anyway? have new_entry form on page, creates new entry via ajax. perhaps interfering sort? appreciated!
the reason why there's no 'stringify_keys' method because you're passing array create action , not sort action. have data-update-url in erb.html file? should sort_entries_path.
Comments
Post a Comment