I have implemented act_as_votable gem and it seems to work correctly. But I'm trying to get the button to work with ajax, and the console says that I get 500 errors in the put method. In addition, I was not sure what to put in for rendering a new image. Any ideas what I'm missing here?
Here is my controller :
def upvote
@medium = Medium.find(params[:id])
if current_member.voted_up_on? @medium
@medium.unliked_by current_member
else
@medium.liked_by current_member
end
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
routes.rb
resources :media do
member do
put "favorite", to: "media#upvote"
end
end
View
<div class="actions_act">
<span id="comment_act_btn media_buttons">
<% if current_member.voted_up_on?(medium) %>
<%= link_to image_tag("Favorite 3.png", class: "act_actions", title: "Comments", alt: "Comments") + 'Favorite', favorite_medium_path(medium), method: :put, :remote => true, :class => "btn favorite" %>
<% else %>
<%= link_to image_tag("Favorite 5.png", class: "act_actions", title: "Comments", alt: "Comments") + 'Favorite', favorite_medium_path(medium), method: :put, :remote => true, :class => "btn favorite" %>
<% end %>
</span>
</div>
upvote.js.erb
$("#comment_act_btn").html("<%= j %>");
Server logs
Started PUT "/activities/4/favorite" for 127.0.0.1 at 2014-02-13 14:23:47 -0600
Processing by ActivitiesController
Parameters: {"id"=>"4"}
[1m[36mMember Load (1.0ms)[0m [1mSELECT "members".* FROM "members" WHERE "members"."id" = 1 LIMIT 1[0m
[1m[35mActivity Load (0.0ms)[0m SELECT "activities".* FROM "activities" WHERE "activities"."id" = ? LIMIT 1 [["id", "4"]]
[1m[36m (0.0ms)[0m [1mSELECT COUNT(*) FROM "votes" WHERE "votes"."voter_id" = 1 AND "votes"."voter_type" = 'Member' AND "votes"."votable_id" = 4 AND "votes"."votable_type" = 'Activity' AND "votes"."vote_scope" IS NULL AND "votes"."vote_flag" = 't'[0m
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 4 AND "votes"."votable_type" = 'Activity' AND "votes"."voter_id" = 1 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'Member'
[1m[36m (1.0ms)[0m [1mbegin transaction[0m
[1m[35mSQL (2.0ms)[0m INSERT INTO "votes" ("created_at", "updated_at", "votable_id", "votable_type", "vote_flag", "vote_scope", "vote_weight", "voter_id", "voter_type") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 13 Feb 2014 20:23:47 UTC +00:00], ["updated_at", Thu, 13 Feb 2014 20:23:47 UTC +00:00], ["votable_id", 4], ["votable_type", "Activity"], ["vote_flag", true], ["vote_scope", nil], ["vote_weight", 0], ["voter_id", 1], ["voter_type", "Member"]]
[1m[36m (127.0ms)[0m [1mcommit transaction[0m
Rendered activities/upvote.js.erb (3.0ms)
Completed 500 Internal Server Error in 151ms
ActionView::Template::Error (wrong number of arguments (0 for 1)):
1: $("#comment_act_btn").html("<%= j %>");
app/views/activities/upvote.js.erb:1:in `_app_views_activities_upvote_js_erb__413243720_47347896'
app/controllers/activities_controller.rb:41:in `upvote'