rails3 ajax toggle attributes
// Structure
rails g scaffold Article name:string approved:boolean
// routes
resources :articles do
get 'toggle_approve', :on => :member
end
// Article Helper
def approve_link_text(approvable)
approvable.approved? ? 'Un-approve' : 'Approve'
end
// Article View
<%= link_to approve_link_text(article), toggle_approve_article_path(article),
:remote => true %>
(OR)
<% @articles.each do |article| %>
<%= article.name %>
<%= link_to approve_link_text(@article), toggle_approve_article_path(@article),
:remote => true,
:id => "approve_link_#{@article.id}" %>
<% end %>
// Article Controller
def toggle_approve
@a = Article.find(params[:id])
@a.toggle!(:approved)
render :nothing => true
end
// toggle_approve.js.erb.
$("#approve_link_<%= @article.id %>")
.text("<%= approve_link_text(@article) %>");
$("#article_<%= @article.id %>").effect("highlight");