Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

RJS with toggle_slide if not visible only

RJS is very powerful. here is a small code snippet that shows how to do a toggle on a div if NOT visible. If the div is visible, it just exchange it. Here is my rhtml code:

    <div style="height: 30px">
        <div style="float: left; padding: 0 0 0 180px">
            <%= link_to_remote("test_visibility" , 
                :with => "'is_visible=' + Element.visible('list_div')",
                :loading => "Element.show('getting_results')",
                :complete => "Element.hide('getting_results')",
                :failure => "alert('An error occured, please email us directly!')",
                :url => { :action => 'one_action' }) %>
        </div>
        <div id="getting_results" style="float: left; padding: 3px 0 0 30px; display: none;">
            <%= image_tag "ajax-loader.gif", :class => "image", :alt => "loading..." %>
        </div>
    </div>
    
    <div id="list_div" style="display: none;" >
    </div>
    


And in my controller, I have the following code:

def one_action
      render :update do | page |
        page.replace_html 'list_div', :partial => 'one_partial'
        if params['is_visible'] == 'false'
          page.visual_effect :toggle_slide, 'list_div', :duration => 2
        end       
      end
end


How it works?
Element.visible('list_div')
is a prototype function that returns false if the element is not visible and true if it is. In the above case, we have the orginial visibility of the div to be "display: none". Thus the first time the action is called, the Ajax call will send the parameter false to the action. This will make the div appear. For subsequent requests, the value of visibility will be true, thus only replacing the div.

how to escape from AJAX URL

From Dylan Stamat's post on the Ruby on Rails list


Within my "login.rjs" template that get's invoked after my login process, it returns to the same page with an error message on error, or... if the login was successful, it needs to "redirect" to another controller... which is pretty much impossible to do otherwise. so, my " login.rjs" looks like:

if @logged_in_client
  page.replace_html "message", :partial => 'shared/bad_login'
else
  page.redirect_to "whatever you want here"
end

------ End Message--------
Here is my two cents:
Technically, you can do this without a RJS template at all, because it is such a simple example. Use this in your controller

render :update do |page|
  if @logged_in_client
    page.replace_html "message", :partial => 'shared/bad_login'
  else
    page.redirect_to "whatever you want here"
  end
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS