使用acts_as_votable进行更新操作的问题

时间:2014-08-21 16:46:48

标签: ruby-on-rails controller voting-system

我目前正在获得投票以访问数据库,但我现在遇到了让更新操作在我的控制器中工作的问题。投票不会记录更新操作,但不会将其记录下来。但是,我为Pits#update丢失了模板错误。任何帮助表示赞赏。感谢。

终端错误

Started PUT "/pits/3" for 127.0.0.1 at 2014-08-21 11:38:25 -0500
Processing by PitsController#update as JS
  Parameters: {"id"=>"3"}
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 4  ORDER BY "users"."id" ASC LIMIT 1
  Pit Load (0.2ms)  SELECT  "pits".* FROM "pits"  WHERE "pits"."user_id" = ? AND "pits"."id" = ? LIMIT 1  [["user_id", 4], ["id", 3]]
Completed 404 Not Found in 64ms

ActiveRecord::RecordNotFound (Couldn't find Pit with 'id'=3 [WHERE "pits"."user_id" = ?]):
  app/controllers/pits_controller.rb:37:in `update'

我目前有

坑控制器

class PitsController< ApplicationController中

def new
  @pit = Pit.new
end

def index
  @pit = Pit.all
  @user = User.find_by(params[:id])
  @pits = Pit.paginate(:page => params[:page]).order('created_at ASC').group_by { |pit| pit.created_at.strftime("%B %Y") }
end




def create
  @user = current_user
  @pit = current_user.pits.create(pit_params)
    if @pit.save
      redirect_to @pit
    else
      render 'new'
    end
end



def show
  @pit = Pit.find(params[:id])
end

def edit
end

def update
   @user = current_user
   @pit = current_user.pits.find(params[:id])
   if @pit.update_attributes(pit_params)
      redirect_to @pit
end
end

private

def pit_params
    params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id)
end

end

评论控制器

class CommentsController < ApplicationController


 def create
  @pit= Pit.find(params[:pit_id])
  @comment = @pit.comments.build(comments_params)
  @comment.user = current_user
  @comment.save

  redirect_to pit_path(@pit)
end

  def destroy
    @pit = Pit.find(params[:pit_id])
    @comment = @pit.comments.find(params[:id])
    @comment.destroy
    redirect_to pit_path(@pit)
end

def upvote
  @pit = Pit.find(params[:pit_id])
  @comment = @pit.comments.find(params[:comment_id])
  @comment.upvote_by current_user
  redirect_to pit_path(@pit)
end

def downvote
  @pit = Pit.find(params[:pit_id])
  @comment = @pit.comments.find(params[:comment_id])
  @comment.downvote_from current_user
  redirect_to pit_path(@pit)
end

def update
end




def show  
end

  private

def comments_params
    params.require(:comment).permit(:body, :user_id, :votable, :voter, :vote_scope)
end

end

_comment.html.erb

  <div class = "well">
    <p>
    <strong>Comment:</strong>
    <%= comment.body %>
    <p>posted by: <%= comment.user.name %></p>
       <%= link_to "Like", pit_comment_like_path(@pit, comment), method: :put , :remote => true %>
       <%= link_to "Dislike", pit_comment_dislike_path(@pit, comment), method: :put, :remote => true %>
  </p>

  <p>
    <%if comment.user == current_user %>

     <%= link_to 'Destroy Comment', [@pit, comment],
                 method: :delete,
                 data: { confirm: 'Are you sure?' } %>
    <% end %>             
  </p>
  </div>

1 个答案:

答案 0 :(得分:1)

不确定您在此处所做的一切,但我怀疑该错误与使用current_user查找pit有关。如果current_user不是坑的user_id,它将找不到任何坑(确切地说是你的错误)。

尝试调整到这个,它应该能够正确地找到坑。

def update
   @pit = Pit.find(pit_params[:id])
   if @pit.update_attributes(pit_params)
      redirect_to @pit
end