Sonal S.
Sonal S.

Reputation: 1442

check Cookies are disabled or not in ruby on rails

I m developing web application using Ruby on Rails. I want to check whether cookies are enabled or not. If not then i m displaying an error message and user cant proceed further.

In application_controller i did this..

before_filter :cookies_required
def cookies_required
    return true unless cookies["_no_app"].blank?        
    render :template => "shared/cookies_required", :layout => "login"
end

And the code in shared/cookies_required.html.erb is...

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<%= stylesheet_link_tag "login.css"%>
<head>
    <style type="text/css">
        body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
        div.dialog {
            width: 25em;
            padding: 0 4em;
            margin: 4em auto 0 auto;
            }
        h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
    </style>
</head>
<body>

<div class="dialog">
<p><font size="4">Cookies are disabled. First enable it.</font> </p>
</div>
</body>
</html>

This code is working fynn... but problem is when the browser first loaded the page, displays error msg tht cookies are disabled, even if it is enabled...

Upvotes: 2

Views: 2960

Answers (1)

Rishabh Sairawat
Rishabh Sairawat

Reputation: 460

Change your method to the following:

def cookies_required
   return true unless request.cookies.blank?  
   render :template => "shared/cookies_required", :layout => "login"
end

Upvotes: 3

Related Questions