Distinguishing between people who have likes to posts and those who have dislikes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I want to distinguish between people who have likes to posts and those who have dislikes. I want to find a way to improve my code without thinking it is efficient.



<% if current_user.voted_up_on? @post %>
<%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-primary" %>
<%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
<% elsif current_user.voted_down_on? @post %>
<%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
<%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
<% else %>
<%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
<%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
<% end %>






share|improve this question



























    up vote
    2
    down vote

    favorite












    I want to distinguish between people who have likes to posts and those who have dislikes. I want to find a way to improve my code without thinking it is efficient.



    <% if current_user.voted_up_on? @post %>
    <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-primary" %>
    <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
    <% elsif current_user.voted_down_on? @post %>
    <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
    <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
    <% else %>
    <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
    <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
    <% end %>






    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to distinguish between people who have likes to posts and those who have dislikes. I want to find a way to improve my code without thinking it is efficient.



      <% if current_user.voted_up_on? @post %>
      <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-primary" %>
      <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <% elsif current_user.voted_down_on? @post %>
      <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
      <% else %>
      <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <% end %>






      share|improve this question













      I want to distinguish between people who have likes to posts and those who have dislikes. I want to find a way to improve my code without thinking it is efficient.



      <% if current_user.voted_up_on? @post %>
      <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-primary" %>
      <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <% elsif current_user.voted_down_on? @post %>
      <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
      <% else %>
      <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
      <% end %>








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 26 at 21:13









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked May 26 at 21:02









      yori

      132




      132




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I cam think of a couple of options. You could simplify this into something like:



          <% if current_user.voted_up_on? @post %>
          <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post),
          <% else %>
          <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
          <% end %>

          <% if current_user.voted_down_on? @post %>
          <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
          <% else %>
          <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
          <% end %>


          However, I usually prefer something like



          <% like_class = current_user.voted_up_on?(@post) ? 'btn-outline-primary' : 'btn-outline-dark' %>
          <% dislike_class = current_user.voted_down_on?(@post) ? 'btn-outline-danger' : 'btn-outline-dark' %>

          <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class:like_class %>
          <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: dislike_class %>


          A few things:



          1. I would probably put this into a helper method.


          2. Usually you would hide or at least disable the like button if the post has already been liked.


          3. You haven't shown your model's code but you need to be careful that you don't query the database twice, once for voted_up_on? and once for voted_down_on?. Since they are mutually exclusive you may want to have a single method that return the state (up, down or none) and use a case statement.






          share|improve this answer




























            up vote
            0
            down vote













            As you can easily identify from your code that link_to code for Like and Dislike are same for all conditions only the class value is changed
            so you can use below code it looks more clean:



            <% like_class = current_user.voted_up_on? @post ? 'btn-outline-primary' : 'btn-outline-dark' %>
            <% dislike_class = current_user.voted_down_on? @post ? 'btn-outline-danger' : 'btn-outline-dark' %>
            <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn #like_class" %>
            <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn #dislike_class" %>





            share|improve this answer





















              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195238%2fdistinguishing-between-people-who-have-likes-to-posts-and-those-who-have-dislike%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              I cam think of a couple of options. You could simplify this into something like:



              <% if current_user.voted_up_on? @post %>
              <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post),
              <% else %>
              <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
              <% end %>

              <% if current_user.voted_down_on? @post %>
              <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
              <% else %>
              <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
              <% end %>


              However, I usually prefer something like



              <% like_class = current_user.voted_up_on?(@post) ? 'btn-outline-primary' : 'btn-outline-dark' %>
              <% dislike_class = current_user.voted_down_on?(@post) ? 'btn-outline-danger' : 'btn-outline-dark' %>

              <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class:like_class %>
              <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: dislike_class %>


              A few things:



              1. I would probably put this into a helper method.


              2. Usually you would hide or at least disable the like button if the post has already been liked.


              3. You haven't shown your model's code but you need to be careful that you don't query the database twice, once for voted_up_on? and once for voted_down_on?. Since they are mutually exclusive you may want to have a single method that return the state (up, down or none) and use a case statement.






              share|improve this answer

























                up vote
                0
                down vote



                accepted










                I cam think of a couple of options. You could simplify this into something like:



                <% if current_user.voted_up_on? @post %>
                <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post),
                <% else %>
                <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
                <% end %>

                <% if current_user.voted_down_on? @post %>
                <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
                <% else %>
                <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
                <% end %>


                However, I usually prefer something like



                <% like_class = current_user.voted_up_on?(@post) ? 'btn-outline-primary' : 'btn-outline-dark' %>
                <% dislike_class = current_user.voted_down_on?(@post) ? 'btn-outline-danger' : 'btn-outline-dark' %>

                <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class:like_class %>
                <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: dislike_class %>


                A few things:



                1. I would probably put this into a helper method.


                2. Usually you would hide or at least disable the like button if the post has already been liked.


                3. You haven't shown your model's code but you need to be careful that you don't query the database twice, once for voted_up_on? and once for voted_down_on?. Since they are mutually exclusive you may want to have a single method that return the state (up, down or none) and use a case statement.






                share|improve this answer























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  I cam think of a couple of options. You could simplify this into something like:



                  <% if current_user.voted_up_on? @post %>
                  <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post),
                  <% else %>
                  <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
                  <% end %>

                  <% if current_user.voted_down_on? @post %>
                  <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
                  <% else %>
                  <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
                  <% end %>


                  However, I usually prefer something like



                  <% like_class = current_user.voted_up_on?(@post) ? 'btn-outline-primary' : 'btn-outline-dark' %>
                  <% dislike_class = current_user.voted_down_on?(@post) ? 'btn-outline-danger' : 'btn-outline-dark' %>

                  <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class:like_class %>
                  <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: dislike_class %>


                  A few things:



                  1. I would probably put this into a helper method.


                  2. Usually you would hide or at least disable the like button if the post has already been liked.


                  3. You haven't shown your model's code but you need to be careful that you don't query the database twice, once for voted_up_on? and once for voted_down_on?. Since they are mutually exclusive you may want to have a single method that return the state (up, down or none) and use a case statement.






                  share|improve this answer













                  I cam think of a couple of options. You could simplify this into something like:



                  <% if current_user.voted_up_on? @post %>
                  <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post),
                  <% else %>
                  <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
                  <% end %>

                  <% if current_user.voted_down_on? @post %>
                  <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-danger" %>
                  <% else %>
                  <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn btn-outline-dark" %>
                  <% end %>


                  However, I usually prefer something like



                  <% like_class = current_user.voted_up_on?(@post) ? 'btn-outline-primary' : 'btn-outline-dark' %>
                  <% dislike_class = current_user.voted_down_on?(@post) ? 'btn-outline-danger' : 'btn-outline-dark' %>

                  <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class:like_class %>
                  <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: dislike_class %>


                  A few things:



                  1. I would probably put this into a helper method.


                  2. Usually you would hide or at least disable the like button if the post has already been liked.


                  3. You haven't shown your model's code but you need to be careful that you don't query the database twice, once for voted_up_on? and once for voted_down_on?. Since they are mutually exclusive you may want to have a single method that return the state (up, down or none) and use a case statement.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 27 at 15:32









                  Marc Rohloff

                  2,56935




                  2,56935






















                      up vote
                      0
                      down vote













                      As you can easily identify from your code that link_to code for Like and Dislike are same for all conditions only the class value is changed
                      so you can use below code it looks more clean:



                      <% like_class = current_user.voted_up_on? @post ? 'btn-outline-primary' : 'btn-outline-dark' %>
                      <% dislike_class = current_user.voted_down_on? @post ? 'btn-outline-danger' : 'btn-outline-dark' %>
                      <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn #like_class" %>
                      <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn #dislike_class" %>





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        As you can easily identify from your code that link_to code for Like and Dislike are same for all conditions only the class value is changed
                        so you can use below code it looks more clean:



                        <% like_class = current_user.voted_up_on? @post ? 'btn-outline-primary' : 'btn-outline-dark' %>
                        <% dislike_class = current_user.voted_down_on? @post ? 'btn-outline-danger' : 'btn-outline-dark' %>
                        <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn #like_class" %>
                        <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn #dislike_class" %>





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          As you can easily identify from your code that link_to code for Like and Dislike are same for all conditions only the class value is changed
                          so you can use below code it looks more clean:



                          <% like_class = current_user.voted_up_on? @post ? 'btn-outline-primary' : 'btn-outline-dark' %>
                          <% dislike_class = current_user.voted_down_on? @post ? 'btn-outline-danger' : 'btn-outline-dark' %>
                          <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn #like_class" %>
                          <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn #dislike_class" %>





                          share|improve this answer













                          As you can easily identify from your code that link_to code for Like and Dislike are same for all conditions only the class value is changed
                          so you can use below code it looks more clean:



                          <% like_class = current_user.voted_up_on? @post ? 'btn-outline-primary' : 'btn-outline-dark' %>
                          <% dislike_class = current_user.voted_down_on? @post ? 'btn-outline-danger' : 'btn-outline-dark' %>
                          <%= link_to "LIKE #@post.get_upvotes.size", like_post_path(@post), method: :post, class: "btn #like_class" %>
                          <%= link_to "DISLIKE #@post.get_downvotes.size", dislike_post_path(@post), method: :post, class: "btn #dislike_class" %>






                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered May 27 at 15:21









                          rahul mishra

                          1111




                          1111






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195238%2fdistinguishing-between-people-who-have-likes-to-posts-and-those-who-have-dislike%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Python Lists

                              Aion

                              JavaScript Array Iteration Methods