# Strange REST routing problem using sortable\_element. Help!

**URL:** <https://www.ruby-forum.com/t/strange-rest-routing-problem-using-sortable-element-help/140831>\
**Category:** Rails\
**Created:** [June 19, 2008, 1:12am UTC](https://www.ruby-forum.com/t/strange-rest-routing-problem-using-sortable-element-help/140831 "2008-06-19T01:12:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [June 19, 2008, 1:12am UTC](https://www.ruby-forum.com/t/strange-rest-routing-problem-using-sortable-element-help/140831/1 "2008-06-19T01:12:36Z")

</div>

Hi,

I’m having a very strange problem using a AJAX sortable list  
containing images for articles. I tried a lot of things and searched  
for hours but I don’t find the reason. I would really appreciate your  
help. Please excuse my deficient english, I’m from Berlin. I try to  
explain the situation as clear as possible. If you need any further  
info, just drop a note.

The idea:  
User can post articles and add comments and images (assets) to each  
article. So every article has many comments and assets. I’m using  
restful routing and AJAX for image uploading, deleting and adding  
comments etc. without any problems.

The problem:  
When I try to reorder the images, all the information is saved, but  
then the index action of the controller is called again. So the page  
reloads. But I’m using Ajax to prevent this. If I have for example a  
form on the page with unsaved information, all user input is lost and  
the page is refreshed with the data from the DB.

The Routing:  
To access the assets of an article (e.g. ID 3) I call /articles/3/  
assets .

My routes.rb looks like this (partial):

map.resources :articles, :member =\> {:publish =\> :get} do |article|  
article.resources :comments do |comment|  
comment.resources :comments  
end

article.resources :assets, :collection =\> {:saveorder =\> :post}  
end

rake routes shows me for the articles the following routes:  
articles GET /  
articles  
{:action=\>“index”, :controller=\>“articles”}  
formatted\_articles GET /  
articles.:format  
{:action=\>“index”, :controller=\>“articles”}  
POST /  
articles  
{:action=\>“create”, :controller=\>“articles”}  
POST /  
articles.:format  
{:action=\>“create”, :controller=\>“articles”}  
new\_article GET /articles/  
new  
{:action=\>“new”, :controller=\>“articles”}  
formatted\_new\_article GET /articles/  
new.:format  
{:action=\>“new”, :controller=\>“articles”}  
edit\_article GET /articles/:id/  
edit  
{:action=\>“edit”, :controller=\>“articles”}  
formatted\_edit\_article GET /articles/:id/  
edit.:format  
{:action=\>“edit”, :controller=\>“articles”}  
publish\_article GET /articles/:id/  
publish  
{:action=\>“publish”, :controller=\>“articles”}  
formatted\_publish\_article GET /articles/:id/  
publish.:format  
{:action=\>“publish”, :controller=\>“articles”}  
article GET /  
articles/:id  
{:action=\>“show”, :controller=\>“articles”}  
formatted\_article GET /  
articles/:id.:format  
{:action=\>“show”, :controller=\>“articles”}  
PUT /  
articles/:id  
{:action=\>“update”, :controller=\>“articles”}  
PUT /  
articles/:id.:format  
{:action=\>“update”, :controller=\>“articles”}  
DELETE /  
articles/:id  
{:action=\>“destroy”, :controller=\>“articles”}  
DELETE /  
articles/:id.:format  
{:action=\>“destroy”, :controller=\>“articles”}  
article\_comments GET /articles/:article\_id/  
comments  
{:action=\>“index”, :controller=\>“comments”}  
formatted\_article\_comments GET /articles/:article\_id/  
comments.:format  
{:action=\>“index”, :controller=\>“comments”}  
POST /articles/:article\_id/  
comments  
{:action=\>“create”, :controller=\>“comments”}  
POST /articles/:article\_id/  
comments.:format  
{:action=\>“create”, :controller=\>“comments”}  
new\_article\_comment GET /articles/:article\_id/  
comments/new  
{:action=\>“new”, :controller=\>“comments”}  
formatted\_new\_article\_comment GET /articles/:article\_id/  
comments/new.:format  
{:action=\>“new”, :controller=\>“comments”}  
edit\_article\_comment GET /articles/:article\_id/  
comments/:id/edit  
{:action=\>“edit”, :controller=\>“comments”}  
formatted\_edit\_article\_comment GET /articles/:article\_id/  
comments/:id/edit.:format  
{:action=\>“edit”, :controller=\>“comments”}  
article\_comment GET /articles/:article\_id/  
comments/:id  
{:action=\>“show”, :controller=\>“comments”}  
formatted\_article\_comment GET /articles/:article\_id/  
comments/:id.:format  
{:action=\>“show”, :controller=\>“comments”}  
PUT /articles/:article\_id/  
comments/:id  
{:action=\>“update”, :controller=\>“comments”}  
PUT /articles/:article\_id/  
comments/:id.:format  
{:action=\>“update”, :controller=\>“comments”}  
DELETE /articles/:article\_id/  
comments/:id  
{:action=\>“destroy”, :controller=\>“comments”}  
DELETE /articles/:article\_id/  
comments/:id.:format  
{:action=\>“destroy”, :controller=\>“comments”}  
article\_comment\_comments GET /articles/:article\_id/  
comments/:comment\_id/comments  
{:action=\>“index”, :controller=\>“comments”}  
formatted\_article\_comment\_comments GET /articles/:article\_id/  
comments/:comment\_id/comments.:format  
{:action=\>“index”, :controller=\>“comments”}  
POST /articles/:article\_id/  
comments/:comment\_id/comments  
{:action=\>“create”, :controller=\>“comments”}  
POST /articles/:article\_id/  
comments/:comment\_id/comments.:format  
{:action=\>“create”, :controller=\>“comments”}  
new\_article\_comment\_comment GET /articles/:article\_id/  
comments/:comment\_id/comments/new  
{:action=\>“new”, :controller=\>“comments”}  
formatted\_new\_article\_comment\_comment GET /articles/:article\_id/  
comments/:comment\_id/comments/new.:format  
{:action=\>“new”, :controller=\>“comments”}  
edit\_article\_comment\_comment GET /articles/:article\_id/  
comments/:comment\_id/comments/:id/edit  
{:action=\>“edit”, :controller=\>“comments”}  
formatted\_edit\_article\_comment\_comment GET /articles/:article\_id/  
comments/:comment\_id/comments/:id/edit.:format  
{:action=\>“edit”, :controller=\>“comments”}  
article\_comment\_comment GET /articles/:article\_id/  
comments/:comment\_id/comments/:id  
{:action=\>“show”, :controller=\>“comments”}  
formatted\_article\_comment\_comment GET /articles/:article\_id/  
comments/:comment\_id/comments/:id.:format  
{:action=\>“show”, :controller=\>“comments”}  
PUT /articles/:article\_id/  
comments/:comment\_id/comments/:id  
{:action=\>“update”, :controller=\>“comments”}  
PUT /articles/:article\_id/  
comments/:comment\_id/comments/:id.:format  
{:action=\>“update”, :controller=\>“comments”}  
DELETE /articles/:article\_id/  
comments/:comment\_id/comments/:id  
{:action=\>“destroy”, :controller=\>“comments”}  
DELETE /articles/:article\_id/  
comments/:comment\_id/comments/:id.:format  
{:action=\>“destroy”, :controller=\>“comments”}  
saveorder\_article\_assets POST /articles/:article\_id/  
assets/saveorder  
{:action=\>“saveorder”, :controller=\>“assets”}  
formatted\_saveorder\_article\_assets POST /articles/:article\_id/  
assets/saveorder.:format  
{:action=\>“saveorder”, :controller=\>“assets”}  
article\_assets GET /articles/:article\_id/  
assets  
{:action=\>“index”, :controller=\>“assets”}  
formatted\_article\_assets GET /articles/:article\_id/  
assets.:format  
{:action=\>“index”, :controller=\>“assets”}  
POST /articles/:article\_id/  
assets  
{:action=\>“create”, :controller=\>“assets”}  
POST /articles/:article\_id/  
assets.:format  
{:action=\>“create”, :controller=\>“assets”}  
new\_article\_asset GET /articles/:article\_id/  
assets/new  
{:action=\>“new”, :controller=\>“assets”}  
formatted\_new\_article\_asset GET /articles/:article\_id/  
assets/new.:format  
{:action=\>“new”, :controller=\>“assets”}  
edit\_article\_asset GET /articles/:article\_id/  
assets/:id/edit  
{:action=\>“edit”, :controller=\>“assets”}  
formatted\_edit\_article\_asset GET /articles/:article\_id/  
assets/:id/edit.:format  
{:action=\>“edit”, :controller=\>“assets”}  
article\_asset GET /articles/:article\_id/  
assets/:id  
{:action=\>“show”, :controller=\>“assets”}  
formatted\_article\_asset GET /articles/:article\_id/  
assets/:id.:format  
{:action=\>“show”, :controller=\>“assets”}  
PUT /articles/:article\_id/  
assets/:id  
{:action=\>“update”, :controller=\>“assets”}  
PUT /articles/:article\_id/  
assets/:id.:format  
{:action=\>“update”, :controller=\>“assets”}  
DELETE /articles/:article\_id/  
assets/:id  
{:action=\>“destroy”, :controller=\>“assets”}  
DELETE /articles/:article\_id/  
assets/:id.:format  
{:action=\>“destroy”, :controller=\>“assets”}

Please note that I’m using acts\_as\_tree for the comments to enable  
replys.

My Controller:  
class AssetsController \< ApplicationController

make\_resourceful do  
actions :show, :new, :edit, :update, :destroy  
belongs\_to :article

before :new do  
@comment.user = current\_user  
end  
end

def index  
if params[:article\_id].nil?  
@assets = Asset.find(:all, :conditions =\> {:parent\_id =\>  
nil}, :order =\> ‘order\_number ASC’)  
else  
@assets = Asset.find(:all, :conditions =\> {:parent\_id =\>  
nil, :article\_id =\> params[:article\_id]}, :order =\> ‘order\_number  
ASC’)  
end  
respond\_to do |format|  
format.html # index.rhtml  
format.xml { render :xml =\> @assets.to\_xml }  
format.js  
end  
end

# This is the action which is called by sortable\_element

def saveorder  
params[:assetlist].each\_with\_index do |id, position|  
asset = Asset.find(id)  
asset.update\_attribute(‘order\_number’, position)  
end  
respond\_to do |format|  
format.js {render :nothing =\> true}  
end  
end

end

My views (I’m using partials and HAML):

# First the index.haml

%h1= \_(“Listing assets”)  
%ul#assetlist  
=render :partial =\> @assets  
=render :partial =\> ‘form’  
=sortable\_element(“assetlist”, :url =\> saveorder\_article\_assets\_path)

# Than the \_asset.haml partial

%li{:id =\> “asset\_#{asset.id}”}  
= link\_to(image\_tag(asset.public\_filename(:thumb))) if asset.image?  
= link\_to(asset.filename, article\_assets\_path(asset))  
= link\_to\_remote \_(“Delete”), :url =\>  
article\_assets\_path(asset), :confirm =\> \_(‘Are you sure?’), :method  
=\> :delete

# the form partial is just for uploading and works perfectly well, so

I guess it’s not interesting here

And last but not least the Mongrel Server output in the console.  
Please note that after the action saveorder the index action is called  
again → the problem.

#######  
Processing AssetsController#saveorder (for 127.0.0.1 at 2008-06-18  
23:21:16) [POST]  
Session ID:  
BAh7BzoMY3NyZl9pZCIlODg2OTc0MmE3ZDFjN2RiNTQxM2Y0ZWQ3ODIyNDVl  
%0AMDMiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh  
%0Ac2h7AAY6CkB1c2VkewA%3D–985a32d778e836c3c207d7b757c6171368ecfe0a  
Parameters: {“action”=\>“saveorder”,  
“authenticity\_token”=\>“8ecfdb4a14b7c399089e0aaeb5db011ce09712c2”,  
“controller”=\>“assets”, “assetlist”=\>[“238”, “234”],  
“article\_id”=\>“1”}  
Asset Columns (0.002068) SHOW FIELDS FROM `assets`  
Asset Load (0.001336) SELECT \* FROM `assets` WHERE (`assets`.`id`  
= 238)  
SQL (0.000160) BEGIN  
Asset Update (0.001113) UPDATE `assets` SET `created_at` =  
‘2008-06-16 20:14:04’, `thumbnail` = NULL, `content_type` = ‘image/  
jpeg’, `user_id` = 25, `order_number` = 0, `height` = 2048, `filename`  
= ‘DSC00005.JPG’, `width` = 1536, `parent_id` = NULL, `updated_at` =  
‘2008-06-18 23:21:16’, `size` = 641111, `article_id` = 1 WHERE `id` =  
238  
SQL (0.000518) COMMIT  
Asset Load (0.000278) SELECT \* FROM `assets` WHERE (`assets`.`id`  
= 234)  
SQL (0.000146) BEGIN  
Asset Update (0.000432) UPDATE `assets` SET `created_at` =  
‘2008-06-16 00:57:52’, `thumbnail` = NULL, `content_type` = ‘image/  
jpeg’, `user_id` = 25, `order_number` = 1, `height` = 1536, `filename`  
= ‘DSC00008.JPG’, `width` = 2048, `parent_id` = NULL, `updated_at` =  
‘2008-06-18 23:21:16’, `size` = 789570, `article_id` = 1 WHERE `id` =  
234  
SQL (0.000492) COMMIT  
Completed in 0.03366 (29 reqs/sec) | Rendering: 0.00008 (0%) | DB:  
0.00654 (19%) | 200 OK [[http://0.0.0.0/articles/1/assets/saveorder](http://0.0.0.0/articles/1/assets/saveorder)]

Processing AssetsController#index (for 127.0.0.1 at 2008-06-18  
23:21:16) [GET]  
Session ID:  
BAh7BzoMY3NyZl9pZCIlODg2OTc0MmE3ZDFjN2RiNTQxM2Y0ZWQ3ODIyNDVl  
%0AMDMiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh  
%0Ac2h7AAY6CkB1c2VkewA%3D–985a32d778e836c3c207d7b757c6171368ecfe0a  
Parameters: {“action”=\>“index”, “controller”=\>“assets”,  
“article\_id”=\>“1”}  
Asset Load (0.000625) SELECT \* FROM `assets` WHERE  
(`assets`.`parent_id` IS NULL AND `assets`.`article_id` = ‘1’) ORDER  
BY order\_number ASC  
Rendering template within layouts/application  
Rendering assets/index  
Asset Columns (0.001937) SHOW FIELDS FROM `assets`  
Rendered assets/\_asset (0.00773)  
Rendered assets/\_asset (0.00256)  
Rendered assets/\_form (0.00238)  
Completed in 0.03758 (26 reqs/sec) | Rendering: 0.01914 (50%) | DB:  
0.00256 (6%) | 200 OK [[http://0.0.0.0/articles/1/assets](http://0.0.0.0/articles/1/assets)]

#### 

I also tried to find the error using firebug and it shows an error in  
the XMLHttpRequest, but as soon as the index is refreshed the console  
is cleared and I don’t get the message.

Any hint or idea is welcome. Please help me here, I’m completely  
stuck!

Thanks.
