0

If session[:shopper] is not set, I want to change the page, but if the session[admin] != "true" I also want to change pages.

In other words, if session[:shopper] or session[:admin] is set, display the page.

How can I deny the first statement of my if?

Here's my code so far:

<%if session[:shopper] %>  (this is what I want to deny)  
  <%puts "shopper IS NIL----------------------------------------------"%>
  <script type="text/javascript">
    window.location.href="/home"  
  </script>
<%elsif session[:admin] != "true"%>
<%puts "ADMIN is NIL----------------------------------------------"%>
  <script type="text/javascript">
    window.location.href="/home"  
  </script>
<%end%>
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Fred Novack
  • 727
  • 9
  • 27

3 Answers3

3

Are you sure this is something you want to do in your view? It looks as though the user could bypass this logic by switching off Javascript. Maybe better to do something like this in your controller:

if session[:shopper].nil?
  logger.info 'shopper is NIL'
  redirect = true
elsif session[:admin] != 'true'
  logger.info 'ADMIN is NIL'
  redirect = true
end

if redirect
  redirect_to '/home'
end
Leo
  • 1,539
  • 17
  • 15
0

You can try if with blank? or empty?

<% if session[:shopper].blank? %>  (this is what I want to deny)  
  <%puts "shopper IS NIL----------------------------------------------"%>
  <script type="text/javascript">
    window.location.href="/home"  
   </script>
<% elsif session[:admin] != "true"%>
 <% puts "ADMIN is NIL----------------------------------------------"%>
  <script type="text/javascript">
    window.location.href="/home"  
 </script>
 <% end %>
dp7
  • 6,651
  • 1
  • 18
  • 37
  • I tried that, but I get the following error: "syntax error, unexpected keyword_elsif, expecting keyword_end '.freeze;elsif session[:admin] != "true" " As it would expect a <%end%> by the end of unless statement – Fred Novack Mar 31 '16 at 21:01
  • @FredericoNovackAmaralPereir you can take help of `blank?` or `empty?` with `if`. – dp7 Mar 31 '16 at 21:10
  • @FredericoNovackAmaralPereir `unless` does not take `elsif` rather it only takes `else` . – dp7 Mar 31 '16 at 21:16
  • `unless` with a `else` is generally frowned upon. it's better to use `if !` – Roman Usherenko Mar 31 '16 at 21:17
  • @RadleyAnaya `if session[:shopper] is not set` - From this statement of Frederico, I understood that we just need to check if it has any value or not. That's why I have not compared with `true`. Please correct me if I am wrong. Thanks – dp7 Mar 31 '16 at 21:21
  • @RadleyAnaya yes, why not. anything can be used as per requirement. I find an awesome answer on this here http://stackoverflow.com/questions/885414/a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails – dp7 Mar 31 '16 at 21:32
0

Well if I understand you correctly you want to redirect if they are not an admin or if they are not signed in as a shopper? Try defined?

I would set a flag. For example:

redirect = false;

<%if session[:shopper] == nil || (defined? session[:shopper] == "nil") %> 

  <%puts "shopper IS NIL----------------------------------------------"%>

  redirect = true;

<%elsif session[:admin] != "true"%>
    redirect = true;
<%end%>

<%if redirect == true %>
  <script type="text/javascript">
    window.location.href="/home"  
  </script>
<%end%>

They get to stay if they are:

  1. signed in as a shopper but not an admin.
  2. signed in as an admin but not a shopper.
  3. not an admin or signed on as a shopper (neither).
  4. signed on as both an admin and shopper.

Other options:

session[:shopper].nil? 
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Radmation
  • 1,486
  • 1
  • 13
  • 30
  • Testing `== nil` is almost always the wrong thing to do, and `nil?` as you suggest is a better plan. I'd also add that most of the time the difference between `nil` and `false` is irrelevant, so `if (x)` is preferable to `if (x.nil?)` unless `x` might be literal `false`. It's not clear what the `defined?` call is here for. It seems redundant and confusing. – tadman Mar 31 '16 at 21:57