Ajax

blizz edited this page Sep 12, 2010 · 7 revisions

How about some ajaxified links or buttons?

Inside your DRYML you can do this


          <%= link_to_remote( "remove project",
              :url => { :action => :rem_proj, :id => this.id } )
          %>  

Which creates a link to call a custom controller method rem_proj. So in the controller we will need:


class UsersController < ApplicationController


  def rem_proj
    mp = Project.find(params["id"])
    mp.status = "hidden"
    mp.save
  end


But what if you want a partial update on the page to make the hidden project disappear? Lets define a tag for the content of this page in app/views/taglib/application.dryml


<def tag="user-projects">

New Projects


<%= link_to_remote( “remove project”,
:update => “userproject”,
:url => { :action => :rem_proj, :id => this.id } )
%>


No projects


And then call it from the show.dryml


<show-page>
  <content-body:>
       <user-projects/>
  </content-body:>
</show-page>

Now for the controller code:


def show @user = current_user @new_projects = @user.new_projects if params["sort"].nil? @new_projects = @user.new_projects.sort {|a,b| a[params["sort"]] <=> b[params["sort"]]} unless params["sort"].nil? end def rem_proj mp = Project.find(params[“id”].to_s) mp.status = “hidden” mp.save @user = current_user @new_projects = @user.new_projects render :partial => “userproject”, :object => @new_projects end

Note the “userproject” we will need a partial template for this.

The template gets named _userporject.dryml and goes in the views/ directory for the controller.
userproject.dryml


<% @new
projects = userproject
%>


Now when the user clicks the link_to_remote control it will ajax a request to the server and then receive and updated partial page with the hidden record removed.

There is more going on here than I have described so email the hobo-users google group if you have questions.