通过link_to更新属性

时间:2013-08-13 13:19:30

标签: ruby-on-rails-3 controller update-attribute

每次用户点击link_to时,方法都应该更新属性。累积belongs_to:用户和:问题

 <%= link_to "+1", controller: "/accumulations", action: "vote_up",id: @question, user_id: current_user, method: "post" %>

def vote_up
 @question = Question.find(params[:id])
 @accumulation = Accumulation.where(user_id: current_user.id, question_id: @question.id )

累积的第3个属性是点:

 @accumulation.point = Accumulation.increment_counter(:point, @accumulation)
 @accumulation.save
 redirect_to :back, notice: "+1"
end

它给了我这样的信息:

未定义的方法`point ='for#ActiveRecord :: Relation:0xaa7fc10

如何更新属性?

1 个答案:

答案 0 :(得分:0)

假设任何给定的用户/问题组合只有一个积累,您可以在查询中添加.first

@accumulation = Accumulation.where(user_id: current_user.id, question_id: @question.id ).first

否则,您尝试在记录集合上设置point而不是单个ActiveRecord对象。