form_remote_tag and RJS

I wrote some code for adding a category to a list of categories on a page, complete with some rjs effects. The code saves the category alright but does not execute any js. I have to refresh to see any changes. Here is what I have been using:

VIEW: list.html.erb

<ul id = "category_list"> <%= render :partial => 'category', :collection => @categories %> </ul>

<% form_remote_tag :url => {:action => 'new'},                    :html => {:id => 'category_form'} do %> Name: <%= text_field 'category', 'name' %> <%= submit_tag "Add" %> <% end %>

CONTROLLER: category_controller.rb

  def new     @category = Category.new(params[:category])     respond_to do |format|       if @category.save          format.js          render :partial => 'category', :object => @category     end

  def list     @categories = Category.find(:all)   end

RJS: new.js.erb

page.insert_html :bottom, :category_list, :partial => 'category', :object => @category page.visual_effect :highlight, "category_#{category.id}" page[:category_form].reset

Has anyone else run into this issue or have any ideas about where I am going wrong?

You can use the :collections parameter to render the list. I don't see a need for new.js.erb.

Use the :update=> parameter for the form tag to get the browser to refresh a section of the screen. Put your partial inside a <div> and pass the id of the <div> to the update parameter.

You can do the highlights etc. as parameters to the form_remote_tag. Look at the documentation for form_remote_tag.

I wrote some code for adding a category to a list of categories on a page, complete with some rjs effects. The code saves the category alright but does not execute any js. I have to refresh to see any changes. Here is what I have been using:

VIEW: list.html.erb

<ul id = "category_list"> <%= render :partial => 'category', :collection => @categories %> </ul>

<% form_remote_tag :url => {:action => 'new'},                   :html => {:id => 'category_form'} do %> Name: <%= text_field 'category', 'name' %> <%= submit_tag "Add" %> <% end %>

CONTROLLER: category_controller.rb

def new    @category = Category.new(params[:category])    respond_to do |format|      if @category.save         format.js         render :partial => 'category', :object => @category    end

def list    @categories = Category.find(:all) end

This can't be exactly what's in your controller because it's
syntatically incorrect. But I'd guess that your new.js.erb file is
never exececuted, since you're telling it to render the partial
instead. On top of that it should be called new.js.rjs, not new.js.erb

Fred

I finally got it working. Many thanks to Fred and Mukund. I changed two things: Firstly instead of putting the div id into an unordered list I made a separate div tag. Secondly I got rid of the render partial line in the controller.