У меня есть три модели: Сообщение (has_many
голосов как votable
), Комментарий (has_many
голосов как votable
) и Голосов (belongs_to
голос).
Это voice_controller.rb:
class VotesController < ApplicationController
def vote_up
@votable = WHAT_TO_PLACE_HERE?.find(params[:id])
if @votable.votes.exists?(:user_id => current_user.id)
@notice = 'You already voted'
else
@vote = @post.votes.create(:user_id => current_user.id, :polarity => 1)
@votable.reload
end
respond_to do |format|
format.js
end
end
Соответствующая схема:
create_table "comments", :force => true do |t|
t.text "content"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "total_votes", :default => 0
end
add_index "comments", ["post_id", "user_id"], :name => "index_comments_on_micropost_id_and_user_id"
create_table "posts", :force => true do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "title"
t.integer "comments_count", :default => 0, :null => false
t.integer "total_votes", :default => 0
end
create_table "votes", :force => true do |t|
t.integer "votable_id"
t.string "votable_type"
t.integer "user_id"
t.integer "polarity"
t.integer "total"
t.datetime "created_at"
t.datetime "updated_at"
end
Мне нужен способ найти тип голосового элемента ( Пост или Комментарий ), чтобы я мог назначить его на @ votable . Любые предложения для достижения этой цели?
EDIT:
просмотров / сообщений / show.html.erb:
<%= link_to "Vote Up", vote_up_path(@post), :remote => true, :class => "vote-up" %><br />
routes.rb:
get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'