Rails:即使我调用了@user.save,属性也不会被保存 [英] Rails: attributes not being saved even though I called @user.save

查看:44
本文介绍了Rails:即使我调用了@user.save,属性也不会被保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行这个函数,我知道它被调用是因为redirect_to 正在工作.但出于某种原因,@user 不是!如果有帮助,@user 是基于设计的.

I'm running this function, and I KNOW that it gets called because the redirect_to is working. But for some reason, @user isn't! If it helps, @user is devise based.

def make_feed_preference
@user = current_user
#@user.feed_preference = params[:preference]

@user.feed_preference = "time"
@user.name = "Shoo Nabarrr"
@user.karma = 666

@user.save

redirect_to '/posts'

结束

我自己修好了.我必须创建一个附加到用户的新类才能使其工作.哈哈.

end

I fixed it myself. I had to create a new class attached to users in order to get it to work. Lol.

推荐答案

您对此用户有任何验证吗?他们可能阻止了这个save.无论 save 是否通过或失败,都会调用 redirect_to.

Do you have any validations on this user? They are probably blocking this save. The redirect_to will be called regardless of whether or not the save passes or fails.

我建议改为这样做:

if @user.save
  redirect_to '/posts'
else
  render :feed_preference
end

其中 :feed_preference 是用户输入其提要偏好的表单.

Where :feed_preference is the form where users enter their feed preferences.

这篇关于Rails:即使我调用了@user.save,属性也不会被保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆