# Where to put my JS stuff?

**URL:** <https://www.ruby-forum.com/t/where-to-put-my-js-stuff/162275>\
**Category:** Rails\
**Created:** [March 8, 2009, 12:48am UTC](https://www.ruby-forum.com/t/where-to-put-my-js-stuff/162275 "2009-03-08T00:48:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathieu\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@mathieu\_r](https://www.ruby-forum.com/u/mathieu_r)\
**Post date:** [March 8, 2009, 12:48am UTC](https://www.ruby-forum.com/t/where-to-put-my-js-stuff/162275/1 "2009-03-08T00:48:23Z")

</div>

Hello,  
I’d link to know your opinion about where should I put some JS logic, In  
a  
form i put it all in the submit button, and in another I put it in the  
controller. See:

_First way:_

Form:  
\<%= submit\_to\_remote ‘Save’, ‘Save’,  
:url =\> {:action =\> ‘update’, :id =\> @folder},  
:update =\> ‘main\_tab\_content’,  
:before =\> ‘loading()’ ,  
:complete =\> “unloading();tree.refreshFolder(’#{@folder.parent.id}’)” %\>

Controller:  
def update  
@folder = Folder.find(params[:id])  
if @folder.update\_attributes(params[:folder])  
flash[:notice] = ‘Folder was successfully updated.’  
redirect\_to :action =\> ‘\_edit’, :id =\> @folder  
else  
render :action =\> ‘edit’  
end  
end

_Second way:_  
\*  
\*  
Form:  
\<%= submit\_to\_remote ‘Create’, ‘Create’,  
:url =\> {:action =\> ‘create’},  
:before =\> ‘loading()’,  
:complete =\> ‘unloading();’ %\>

Controller:  
def create  
parent = Folder.find\_by\_id(params[:target\_id])  
@folder = parent.children.create(params[:folder])  
if parent && @folder.save  
flash[:notice] = ‘Folder was successfully created.’  
render :update do |page|  
page.replace\_html ‘main\_tab\_content’, :partial =\> ‘edit’  
page.call ‘tree.refreshFolder’, @folder.parent.id.to\_s  
end  
else  
render :action =\> ‘new’  
end  
end

First way let the controller cleaner and the submit call more complex.  
Second way is the opposite.  
What’s the preferred way? What would make the application easier to  
maintain  
at the end?

Thanks

Mathieu
