Forum: Ruby on Rails Where to put my JS stuff?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca0d1fa83b8d8f39a51c0f25b0f17336?d=identicon&s=25 Mathieu Rousseau (Guest)
on 2009-03-08 00:48
(Received via mailing list)
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
This topic is locked and can not be replied to.