问题描述
我有点新的Rails 3.1。而我只是在我的生产ENV与我的注册表格(实际上,它更多的是控制器)面临的一个问题。
下面是code。在用户
类UsersController中<的ApplicationController
[...]
DEF创建
@user = User.new(PARAMS [:用户])
logger.info登录在参数的值:#{PARAMS [:用户] [:登录]}# - >登录实际登录
logger.info登录值:#{@user.login}# - >登录空
@ user.admin = FALSE
如果@ user.save
闪光[:通知] = T('flash.notice.user.create.valid)
redirect_back_or_default root_path
其他
闪光[:通知] = T('flash.notice.user.create.invalid)
渲染:行动=> :新
结束
结束
结束
另外,控制器日志显示params哈希表是好
参数:{UTF8=>中✓
authenticity_token=>中QwOqmp0CT / d4mmC1yiLT4uZjP9bNDhbUXHanCQy5ZrA =,
用户=> {登录=>中myLogin
电子邮件=>中[email protected]
密码=>中[FILTERED],
password_confirmation=>中[FILTERED]}}
我的登录表单按预期工作(已创建的用户能够登录)
此外,这只是发生在生产。
编辑:这是我的用户模型
类用户的LT;的ActiveRecord :: Base的
acts_as_authentic
#==回调
before_create:set_defaults
attr_accessible:头像#####编辑:要解决此问题,添加其他领域以及
保护
高清set_defaults
self.total_1 = self.total_2 = self.total_3 = 0
结束
结束
只是为了纪念从上述评论的答案:
通常情况下,你可以使用质量分配到一个模型中设置的字段,但是当你用 attr_accessible
,你再仅限于大规模分配的领域。所以,这样的东西 User.new(PARAMS [:用户])
将无法正常工作;相反,你必须做的:
@user = User.new
@ user.login = PARAMS [:用户] [:登录]
# ...等等。
@ user.save
简单的添加字段到 attr_accessible
列表,你可以回去质量分配。
I'm kind of new to Rails 3.1. and I'm facing an issue only in my production env with my Signup form (actually, it's more about the controller).
Here is the code in User
class UsersController < ApplicationController
[...]
def create
@user = User.new(params[:user])
logger.info "value of login in param : #{params[:user][:login]}" #-> log the actual login
logger.info "value of login : #{@user.login}" #-> log empty
@user.admin = false
if @user.save
flash[:notice] = t('flash.notice.user.create.valid')
redirect_back_or_default root_path
else
flash[:notice] = t('flash.notice.user.create.invalid')
render :action => :new
end
end
end
Also, the controller logs show that the params hash is good
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"QwOqmp0CT/d4mmC1yiLT4uZjP9bNDhbUXHanCQy5ZrA=",
"user"=>{"login"=>"myLogin",
"email"=>"[email protected]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"}}
My login form works as expected (already created users are able to sign in)
Again, this only happens in production.
EDIT: Here is my User Model
class User < ActiveRecord::Base
acts_as_authentic
#== Callbacks
before_create :set_defaults
attr_accessible :avatar ##### EDIT: TO FIX THE ISSUE, ADD THE OTHER FIELDS AS WELL
protected
def set_defaults
self.total_1 = self.total_2 = self.total_3 = 0
end
end
Just to memorialize the answer from the comments above:
Normally you can use mass assignment to set fields on a model, but when you use attr_accessible
, you are then limited to only mass assigning those fields. So stuff like User.new(params[:user])
won't work; instead, you'd have to do:
@user = User.new
@user.login = params[:user][:login]
# ...etc.
@user.save
Simple add your fields to the attr_accessible
list and you can go back to mass assignment.
这篇关于在UsersController中#创建,User.new(PARAMS [:用户])返回一个空的用户(PARAMS看起来不错)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!