Rails acts_as_votable ajax不工作

时间:2014-02-13 08:50:42

标签: jquery ruby-on-rails ajax

我已经实现了acts_as_votable gem,它似乎正常工作。但我试图让按钮与ajax一起使用,控制台说我在put方法上遇到500错误。此外,我不确定要渲染新图像的内容。我在这里缺少什么想法?

这是我的控制器

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

查看

<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  %>");

服务器日志

Started PUT "/activities/4/favorite" for 127.0.0.1 at 2014-02-13 14:23:47 -0600
Processing by ActivitiesController#upvote as JS
  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'

1 个答案:

答案 0 :(得分:0)

将按钮放入其中,使部分制作更容易。

媒体/ _media.html.erb

<span id="med_fav_<%= medium.id %>_<%= medium.member.id %>">
    <%= render :partial => "media/favorite", :locals => {:medium => medium} %>
</span>

媒体/ _favorite.html.erb

<% if medium.votes.size > 0 && medium.votes.size <= 99 && current_member.voted_up_on?(medium) %>
  <%= link_to favorite_medium_path(medium), remote: true, method: :put, :class => "btn favorite" do %>
      <%= image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") %><%= medium.votes.size %>
  <% end %>
<% elsif medium.votes.size > 99 && current_member.voted_up_on?(medium) %>
  <%= link_to image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") +'99+', favorite_medium_path(medium), remote: true, method: :put, :class => "btn favorite" %>
<% elsif medium.votes.size > 0 && medium.votes.size <= 99 %>
  <%= link_to favorite_medium_path(medium), remote: true, method: :put, :class => "btn favorite" do %>
      <%= image_tag("Favorite 5.png", class: "act_actions", title: "Favorite", alt: "Favorite") %><%= medium.votes.size %>
  <% end %>
<% elsif medium.votes.size > 99 %>
  <%= link_to image_tag("Favorite 5.png", class: "act_actions", title: "Favorite", alt: "Favorite") +'99+', favorite_medium_path(medium), remote: true, method: :put, :class => "btn favorite" %>
<% else %>
  <%= link_to image_tag("Favorite 5.png", class: "act_actions", title: "Favorite", alt: "Favorite"), favorite_medium_path(medium), remote: true, method: :put, :class => "btn favorite" %>
<% end %>

媒体/ upvote.js.erb

$("#med_fav_<%= @medium.id %>_<%= @medium.member.id %>").html("<%= escape_javascript(render :partial => 'media/favorite', :locals => {:medium => @medium}) %>");