ruby - rails: upvoted pin by user (exists? relation) -
ruby - rails: upvoted pin by user (exists? relation) -
so have pretty simple upvote scheme in rails app, allows users upvote pin:
pins_controller.rb
def upvote @pin = pin.friendly.find(params[:id]) if @pin.votes.create(user_id: current_user.id) flash[:notice] = "thanks recommendation." redirect_to(pin_path) else redirect_to(pin_path) end end
and in view can upvote clicking on button (represented icon): in app/views/pins/index.html.erb
<%= link_to upvote_pin_path(pin), method: :post %> <span class="text-decoration: none;"><i class="fa fa-chevron-up"></i> <% end %> <%= pluralize(pin.votes.count, "") %></span>
i render icon in different color if user has upvoted pins: thought using exists?:
<%= link_to upvote_pin_path(pin), method: :post %> <% if pin.votes.where(user_id: current_user.id).exists? %> <i class="fa fa-chevron-up" style="color:red"></i> <% else %> <i class="fa fa-chevron-up"></i> <% end %> <% end %>
but doesn't work, instead icons appears red, ideas?
the problem checking whether table exists (check exists? documents).
what want check if empty?
ruby-on-rails ruby vote vote-up-buttons
Comments
Post a Comment