Search This Blog

Friday, October 30, 2009

RJS - Made Easy With Examples


Rails RJS examples.   
 
With RJS we can give a better look our site.

I will show different examples which can help you to use
RJS for your site.

::Examples:: ------------

1) Show,Hide functionality with RJS.   
<%= link_to_function "Edit" do |page|
page[:postbulletin].show; 
page[:error_messages].hide; %>

2) How to pass value to the specific field with RJS.  
<%= link_to_function "Edit" do |page|
page[:bulletin_title].value = bulletin.title; %>

 3) Write condition in block while using RJS.     
<%= link_to_function "Edit" do  |page|
(bulletin.video_id != 0) ?
page[:addupdatevideo1].hide : page[:addupdatevideo1].show; %>

4) Pass patial with RJS from view side.   
<%= link_to_function "Edit" do |page| 
[:addupdatevideo].innerHTML =
render :partial => 'users/get_video'; %>

5) Add and remove class to the specific div or html tag with RJS.  
<%= link_to_function "Edit" do  |page| 
page[:a_sort_by_music].removeClassName("selected");
page["a_sort_by_#{info}"].addClassName("selected"); %>  

6) Remove div from your view and give visual effect from
your view with RJS.     
<%= link_to_function "Edit" do |page|
page[:editvideodetail].remove %> 
<%= link_to_function "Edit" do |page|
page["editvideodetail"].visual_effect(:highlight); %> 

7) Replace content of your div with RJS.  
<%= link_to_function page.replace "showvideodetail",
:partial => 'edit_video_detail'; %> 

 8) Insert a content to specific div with RJS with condition.  
<%= link_to_function "Edit" do |page|   
page["video_#{@video.id}"].innerHTML = 
@video.is_featured ?  "This video has been featured." :
"This video has been unfeatured."; %>

9) If condition with RJS.  

render :update do |page| 
page << "if ($('editvideodetail')) {" 
page.replace "editvideodetail", :partial => 'show_video_detail' 
page["showvideodetail"].visual_effect(:highlight) 
page << "} else { if ($('showvideodetail')) {" 
page[:showvideodetail].remove 
page << "}" 
page.insert_html :after,"video_id_#{params[:id]}", :partial => 'show_video_detail' 
page["video_id_#{params[:id]}"].visual_effect(:highlight) 
page << "}" 
end  

No comments:

Post a Comment