Solution 1 (user can multiple vote)
You could using if.. else..
statement on your view microphoto.
See Listing 10.46. Adding a delete link to the micropost partial.
Micropost can be delete only by owner user.
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
data: { confirm: "You sure?" },
title: micropost.content %>
<% end %>
Example (not tested but i hope this help) :
# on view (e.g show.html.erb)
<% if signed_in? && current_user.id != feed_item.user_id %>
<%= link_to "vote up", vote_up_microphotos_url(feed_item.id), :method => :put %>
<%= link_to "vote down", vote_down_microphotos_url(feed_item.id), :method => :put %>
<% end %>
# controller
before_filter :signed_in_user, only: [:create, :destroy, :vote_up, :vote_down]
def vote_up
@microphoto = Microphoto.find(params[:id])
@microphoto.update_attribute(:votes_up, @microphoto.votes_up + 1)
redirect_to root_path
end
def vote_down
@microphoto = Microphoto.find(params[:id])
@microphoto.update_attribute(:votes_down, @microphoto.votes_down + 1)
redirect_to root_path
end
# route
resources :microphotos do
collection do
put '/vote_up/:id' => "microphotos#vote_up", :as => :vote_up
put '/vote_down/:id' => "microphotos#vote_down", :as => :vote_down
end
end
Solution 2 (user can't multiple vote)
If you want user can't multiple vote on microphoto, you should make a separate model with microphoto model
to vote and vote model
have a relationship with the microphoto model
, relationship looks like :
class User < ActiveRecord::Base
has_many :vote_photos
has_many :microphotos
end
class Microphoto < ActiveRecord::Base
has_many :vote_photos
belongs_to :user
end
class VotePhoto < ActiveRecord::Base
belongs_to :microphoto
belongs_to :user
end