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

Clash 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 %>
ruby ruby-on-rails
add a comment |Â
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 %>
ruby ruby-on-rails
add a comment |Â
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 %>
ruby ruby-on-rails
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 %>
ruby ruby-on-rails
edited May 26 at 21:13
Jamalâ¦
30.1k11114225
30.1k11114225
asked May 26 at 21:02
yori
132
132
add a comment |Â
add a comment |Â
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:
I would probably put this into a helper method.
Usually you would hide or at least disable the like button if the post has already been liked.
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 forvoted_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.
add a comment |Â
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" %>
add a comment |Â
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:
I would probably put this into a helper method.
Usually you would hide or at least disable the like button if the post has already been liked.
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 forvoted_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.
add a comment |Â
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:
I would probably put this into a helper method.
Usually you would hide or at least disable the like button if the post has already been liked.
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 forvoted_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.
add a comment |Â
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:
I would probably put this into a helper method.
Usually you would hide or at least disable the like button if the post has already been liked.
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 forvoted_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.
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:
I would probably put this into a helper method.
Usually you would hide or at least disable the like button if the post has already been liked.
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 forvoted_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.
answered May 27 at 15:32
Marc Rohloff
2,56935
2,56935
add a comment |Â
add a comment |Â
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" %>
add a comment |Â
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" %>
add a comment |Â
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" %>
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" %>
answered May 27 at 15:21
rahul mishra
1111
1111
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password