Best way to display something based on if user logged in or not

CodeCrack

I have an if statement that displays certain link based if a user voted or not for something. However, I'll get an error if the user is not logged in since current_user will be nil. I'm using devise for authentication.

undefined method `voted_as_when_voted_for' for nil:NilClass

               <% if current_user.voted_as_when_voted_for @pin %>                                                                                                                                                
                  <%= link_to dislike_pin_path(@pin), method: :put, class: "btn btn-default" do %>                                                                                                                
                      <span class="glyphicon glyphicon-heart"></span>                                                                                                                                                                   <%= @pin.get_upvotes.size %>                                                                                                                                                                
                  <% end %>                                                                                                                                                                                       
                <% else %>                                                                                                                                                                                        
                  <%= link_to like_pin_path(@pin), method: :put, class: "btn btn-default" do %>                                                                                                                   
                      <span class="glyphicon glyphicon-heart"></span>                                                                                                                                             
                      <%= @pin.get_upvotes.size %>                                                                                                                                                                
                  <% end %>                                                                                                                                                                                       
                <% end %>                                                                                                                                                                                         
                <%= link_to pins_path, class: "btn btn-default" do %>                                                                                                                                             
                    <span class="glyphicon glyphicon-step-backward"></span>                                                                                                                                       
                    Back                                                                                                                                                                                          
                <% end %>                                                                                                                                                                                         
                <% if user_signed_in? %>                                                                                                                                                                          
                  <%= link_to "Edit", edit_pin_path, class: "btn btn-default" %>                                                                                                                                  
                  <%= link_to "Delete", pin_path, method: :delete, data: { confirm: "Are you sure"}, class: "btn btn-default" %>                                                                                  
                <% end %>           

What can I add to that if statement to display one of the links even if the user is not logged in without repeating link_to code. Using DRY Rails methodology. (if a user is not logged in and clicks like/dislike link, it will ask him to log in since I have my routes set up like this:

before_action :authenticate_user!, except: [:index, :show] 
patrick

Try this:

<% if current_user && current_user.voted_as_when_voted_for @pin %>
   ... code here
<% else %> 
  <%= link_to like_pin_path(@pin), method: :put, class: "btn btn-default" do %>                                                                                                                   
    <span class="glyphicon glyphicon-heart"></span>                                                                                                                                             
    <%= @pin.get_upvotes.size %>                                                                                                                                                                
  <% end %> 
<% end %>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Best way to display something live with jQuery

From Dev

How to display data based on current logged user?

From Dev

Is there a way to default your ssh user to something other than your currently logged in username?

From Dev

show logged in content in the best way

From Dev

Is symbolic link to ~/something dependent on logged user?

From Dev

Is symbolic link to ~/something dependent on logged user?

From Dev

What's the best way to get other properties of the logged in user in Java Spring?

From Dev

What is the best way to convert all date time fields in c# to the time zone of the logged in user?

From Dev

Populating textbox based on user logged in

From Dev

A way to display Google Analytics data for the currently logged-in Wordpress user's website?

From Dev

Meteor - IF is a facebook user then display something

From Dev

Meteor - IF is a facebook user then display something

From Dev

Best way to display GUI

From Dev

AngularJs - best way to limit access to 'logged in' users

From Dev

Do something based on user input

From Dev

Best way to authenticate user

From Dev

Best way to alert user

From Java

Display only the projects of the logged-in user

From Dev

how to display attributes of logged in user in yii

From Dev

How to Display The Current Logged In User Firebase

From Dev

How to display the membership expiration date of the logged in user

From Dev

How to display payment method to logged in user only?

From Dev

Allow DISPLAY access for a user not logged on GUI

From Dev

How to display data from currently logged in user

From Dev

Laravel 5.2 : Do something after user has logged in?

From Dev

php form action based on user logged in or not

From Java

Different response based on logged user role

From Dev

Apartment switch tenant based on logged user

From Dev

Limiting Form Options Based on Relation to Logged In User

Related Related

  1. 1

    Best way to display something live with jQuery

  2. 2

    How to display data based on current logged user?

  3. 3

    Is there a way to default your ssh user to something other than your currently logged in username?

  4. 4

    show logged in content in the best way

  5. 5

    Is symbolic link to ~/something dependent on logged user?

  6. 6

    Is symbolic link to ~/something dependent on logged user?

  7. 7

    What's the best way to get other properties of the logged in user in Java Spring?

  8. 8

    What is the best way to convert all date time fields in c# to the time zone of the logged in user?

  9. 9

    Populating textbox based on user logged in

  10. 10

    A way to display Google Analytics data for the currently logged-in Wordpress user's website?

  11. 11

    Meteor - IF is a facebook user then display something

  12. 12

    Meteor - IF is a facebook user then display something

  13. 13

    Best way to display GUI

  14. 14

    AngularJs - best way to limit access to 'logged in' users

  15. 15

    Do something based on user input

  16. 16

    Best way to authenticate user

  17. 17

    Best way to alert user

  18. 18

    Display only the projects of the logged-in user

  19. 19

    how to display attributes of logged in user in yii

  20. 20

    How to Display The Current Logged In User Firebase

  21. 21

    How to display the membership expiration date of the logged in user

  22. 22

    How to display payment method to logged in user only?

  23. 23

    Allow DISPLAY access for a user not logged on GUI

  24. 24

    How to display data from currently logged in user

  25. 25

    Laravel 5.2 : Do something after user has logged in?

  26. 26

    php form action based on user logged in or not

  27. 27

    Different response based on logged user role

  28. 28

    Apartment switch tenant based on logged user

  29. 29

    Limiting Form Options Based on Relation to Logged In User

HotTag

Archive