0

我正在使用acts_as_votable gem。

这是我投票时的日志:

Started PUT "/stories/11/vote" for 127.0.0.1 at 2013-10-09 15:50:58 +0800
Processing by StoriesController#vote as JSON
  Parameters: {"id"=>"11"}
  Story Load (0.6ms)  SELECT "stories".* FROM "stories" WHERE "stories"."id" = $1 LIMIT 1  [["id", "11"]]
  User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 14 ORDER BY "users"."id" ASC LIMIT 1
  Persona Load (0.4ms)  SELECT "personas".* FROM "personas" WHERE "personas"."id" = $1 LIMIT 1  [["id", 15]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = 15 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'Persona'  [["votable_id", 11], ["votable_type", "Story"]]
  ActsAsVotable::Vote Load (3.4ms)  SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = 15 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'Persona' ORDER BY "votes"."id" ASC LIMIT 1  [["votable_id", 11], ["votable_type", "Story"]]
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2  [["votable_id", 11], ["votable_type", "Story"]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 11], ["votable_type", "Story"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 11], ["votable_type", "Story"]]
   (0.3ms)  BEGIN
   (0.2ms)  COMMIT
Completed 500 Internal Server Error in 34ms (Views: 1.6ms | ActiveRecord: 8.0ms)

这是我的 JS:

$('.story-vote-up').on("ajax:success", function(evt, data, status, xhr) {
    console.log(data);
  }).on("ajax:error", function(evt, data, status, xhr){
    console.log(data);
  });

和link_to:

<%= link_to 'Vote', vote_story_path(story), method: :put, remote: true %>

和我的控制器动作:

  def vote
    @story = Story.find(params[:id])
    respond_to do |format|
        if @story.liked_by default_persona
          format.html { redirect_to @story, notice: 'Voted' }
          format.json { render json: @story, status: :voted, location: @story }
        else
          format.html { redirect_to @story, notice: 'Error voting' }
          format.json { render json: @story.errors, status: :unprocessable_entity }
        end
      end
  end

我能够在我想要的响应中获取数据,但为什么状态为 500?

4

1 回答 1

1

对于您返回的成功分支,status: :voted它会导致 500 作为其未注册 Rails 的状态代码。

这是整个 rails 符号到代码的映射(2008 链接)http://www.codyfauser.com/2008/7/4/rails-http-status-code-to-symbol-mapping

或者干脆在 number 中使用所需的状态码status: 200

于 2013-10-09T08:22:07.450 回答