সোমবার, ২৮ ডিসেম্বর, ২০০৯

redirect back in rails

The back button is most commonly used button in web browser.
but it becomes pain to developers very often specially when forms are submitted (in post method)

A back button in web page is useful rather using the browser back button:

here is a simple tip for using back button in web page:

#in appplication_controller.rb (for earlier version application.rb )
before_filter :store_location


private:

def store_location
session[:return_to] = request.env['HTTP_REFERER']
session[:previous_request_method] = request.method
#to avoid redirect to post url
end



#in application_helper.rb

def link_to_back_or_default_path(*args)
text = args.first || 'Back'
default_path = args.second || default_path
#default_path can be root_path or any other fixed path
html_options = args.third
if session[:previous_request_method].to_s =='post' || session[:return_to] == ''
redirect_path = default_path
else
redirect_path = session[:return_to]
end
link_to(text, redirect_path, html_options)
end

#in view

<%=link_to_back_or_default_path('back', 'http://boo.com', :class=>'foo, :id=>'bar')%>

or simply
<%=link_to_back_or_default_path%>


Share your thoughts

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন