Ruby on rails 尝试在Rails中实现非常简单的点
请参阅我的控制器中的以下代码Ruby on rails 尝试在Rails中实现非常简单的点,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,请参阅我的控制器中的以下代码 def create @wad = current_user.wads.build(wad_params) if @wad.save current_user.points == current_user.points + 5 redirect_to @wad else flash[:error] = 'Error try again'
def create
@wad = current_user.wads.build(wad_params)
if @wad.save
current_user.points == current_user.points + 5
redirect_to @wad
else
flash[:error] = 'Error try again'
render 'new'
end
end
我的目标是,当用户创建wad时,他们会得到5分。我将points列迁移到用户表fine中,并能够通过控制台更新points
我没有收到任何错误,WAD正在像往常一样创建,页面重定向正确,但用户表中的points值没有更新。有什么问题吗?这里有几点:
首先,对用户的更改目前不会持久化,因为您没有保存任何一条记录
此外,您需要使用单个=
来分配用户的点,或者使用以下示例中的方法。以下代码将克服此问题:
def create
@wad = current_user.wads.build(wad_params)
if @wad.save
current_user.update_attributes(points: current_user.points + 5) # update_attributes assigns the attributes and saves
redirect_to @wad
else
flash[:error] = 'Error try again'
render 'new'
end
end
=
是一种逻辑比较。试试=
。您可能还需要。保存当前用户