将Ajax与Rails链接一起使用

I am using link_to tag to change the validity:

  <%= link_to "Mark as " + (doc.is_valid ? "invalid" : "valid"), 
                  :action =>'change_validity',:id => doc.id %>

Here, is_valid is a field in a table with boolean value. When it is true link will show as "Mark as invalid". When I click the link it will call the method "change_validity" method in controller. The method will toggle the is_valid field and show "Mark as valid" in view.

This one I want to do using AJAX. I tried to using link_to_remote. But I couldn't get it. Can anyone explain how to do it???

link_to_remote is not available in Rails 3. Add :remote => true to your link.

link_to "Mark as " + (doc.is_valid ? "invalid" : "valid"), 
              change_validity_path(:id => doc.id), :remote => true

EDIT: for rails < 3 try

link_to_remote(
        "Mark as " + (doc.is_valid ? "invalid" : "valid"),
        :url => {:action => "change_validity", :id => doc.id},
        :update => "your_div_id",
        :html => {:class => "something"}
      )

Make one partial page. _preview.html.erb and put below code into your partial view

<%= link_to_remote "Mark as " + (doc.is_valid ? "invalid" : "valid"), :update => "update", :url => { :action => "change_validity", :id => doc.id } %>

In your main view file.put below code

<div id="update">
    <%= render :partial => "preview", :locals => { :doc => @doc} %>
</div>

In your controller should have below code

def change_validity
// do stuff here
render :partial => "preview", :locals => { :doc => @doc}, :content_type => 'text/html'
end