Where to put my JS stuff?

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](http://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