Flowdock

Notes posted by Milind

RSS feed
August 5, 2014
0 thanks

Handling flash for ajax calls

This is helper method which can be easily used to handle flash for ajax calls

##call it in your js.erb
def flash_display
response = ""
flash.each do |name, msg|
	msg=msg+"<button type='button' class='close' title='hide' data- dismiss='alert'><i class='fa-times-circle-o fa pull-right'></i></button>".html_safe
  response = response + 
  content_tag(:div, msg, :id => "flash_# {name}",:class=>"alert alert-danger") do 
  	"#{msg}".html_safe  
  end 	
end
flash.discard
response
end

so in your controller,

def get_data_and_update_div
 @user_details=User.get_details
 if @user_details.nil?
   flash.now[:error]="Add your details and then submit"
   format.js { render   'shared/error_messages'}
 end

in error_messages.js.erb

$('#flash_messages').html("<%= escape_javascript raw(flash_display) %>");

in view file

<div id="flash_messages">
  <% if flash[:error] %>
   <%= content_tag :div, flash['value'], :id =>"flash_error" %>
 <% end %>
</div>
August 5, 2014
0 thanks

include respond_to in your controller action only if there are multiple formats of that view

consider this

def index
 @users=User.get_users
  respond_to do |format|
   format.html
   format.json
   format.js
  end
end

is good if you have a call to users/index by both

<%= link_to ("Show users",user_path)%>
##will render users/index.html.erb
===Also same call but with ajax
<%= link_to ("Show users",user_path,remote=>true)%>
 ##will render users/index.js.erb..handled by respond_to   block
===Also same call but with json
<%= link_to ("Show users",user_path,remote=>true,:format=>json)%>
##will render users/index.json.erb..handled by respond_to   block.

But if you have just first one,so remove respond_to block as you are sure that you only need index.html.erb ALWAYS

def index
  @users=User.get_users
end 

So if your action is being called both by BOTH ajax,non-ajax call,its good to use respond_to.

August 5, 2014
0 thanks

Using render to handle ajax call using same js.erb(DRY)

Suppose your application have many pages using some common view and is updated using ajax,so you can use a single js in multiple views to avoid duplication

format.js { render 'profile/show_user_details' }

And in my profiles/show_user_details.js i can use conditions instead of creating regular partials

<% if params[:controller]== "dashboard"%>
 $("admin_panel").show();
 $("user_panel").hide();
<% elsif params[:controller]== "user"%>
 $("admin_panel").hide();
 $("user_panel").show();
<% elsif params[:controller]== "video"%>
 $("admin_panel").hide();
 $("user_panel").hide();
 $("video_panel").show();
<% else %>
 $("admin_panel").hide();
 $("user_panel").hide();
 $("video_panel").hide();
<%end%>
August 5, 2014
0 thanks

simple use

Examples

<%= @user.created_at.to_date.to_formatted_s(:long_ordinal)%> => July 5th, 2014

OR

<%=@user.created_at.strftime("%b %d,%Y") %> =>  Jul 05,2014

quick ref:-

:db           # => 2008-12-25 14:35:05
:number       # => 20081225143505
:time         # => 14:35
:short        # => 25 Dec 14:35
:long         # => December 25, 2008 14:35
:long_ordinal # => December 25th, 2008 14:35
:rfc822       # => Thu, 25 Dec 2008 14:35:05 +0000