I want to make a generic voting controller for lots of different controllers.
I'm using the Thumbs_up gem which was formerly the vote_fu gem.
https://github.com/kitop/thumbs_up/blob/master/lib/acts_as_voter.rb
My form looks like so which is a partial with the object @voteable:
<strong class="result">Votes: <%= voteable.votes_for - voteable.votes_against %></strong>
<%= form_tag user_votes_path(current_user) do |f| %>
<%= radio_button_tag :thumb_direction, :up %>
<%= radio_button_tag :thumb_direction, :down %>
<%= hidden_field_tag :voteable, @voteable %>
<%= submit_tag :vote %>
<% end %>
However when I try passing the voteable object into the controller directly it doesn't work.
undefined method `base_class' for String:Class
My question is then how to polymorphically look up the same object... ie pass the voteable_type and _id instead of the object itself... Unless there is some other easier way?
Controller looks like this
def create
#@user = User.find(params[:user_id])
current_user.vote(params[:voteable], :direction => params[:thumb_direction], :exclusive => true)
end
#routes
resources :users do
resources :votes
end
Did something like this
def create
voteable_class = params[:voteable_type].constantize
voteable_id = (params[:voteable_type].downcase + "_id").to_sym
voteable_instance = voteable_class.find(params[voteable_id])
current_user.vote(voteable_instance, :direction => params[:thumb_direction], :exclusive => true)
redirect_to :back
end
And changed my routes for the nested voting resource on each model I wanted to use it.
<%= form_tag [voteable, Vote.new] do |f| %>
<%= radio_button_tag :thumb_direction, :up %>
<%= radio_button_tag :thumb_direction, :down %>
<%= hidden_field_tag :voteable_type, voteable.class %>
<%= submit_tag :vote %>
<% end %>