2013-03-22 30 views
0

我向应用程序添加了这一段代码,现在它不允许任何人注册。我收到以下错误:分配多参数属性时出现1个错误。错误页面的标题是“UsersController#create中的ActiveRecord :: MultiparameterAssignmentErrors”。不幸的是,我不确定如何解决其他帖子无用的问题。任何人有一些解决方案date_select的多参数属性错误

new.html.erb:

<div class="field"> 
        <%= f.label :birthday %> 
        <%= f.date_select :birthday, :start_year => 1995, :end_year => 1930 %> 
        </div> 

users_controller:

def create 
    @user = User.new(params[:user]) 
    if @user.save 
     UserMailer.registration_confirmation(@user).deliver 
     session[:user_id] = @user.id 
     redirect_to root_url, notice: "Thank you for signing up!" 
    else 
     render "new" 
    end 
    end 

development.log:

Started POST "/users" for 127.0.0.1 at 2013-03-22 10:27:31 -0400 
Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"7KAgvcc6yvuhKQGNrJo8UpfsUyuNG16TuMsRj6qst48=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "username"=>"james", "zip_code"=>"84784", "gender"=>"women", "age"=>"23", "age_end"=>"39", "birthday(1i)"=>"1995", "birthday(2i)"=>"3", "birthday(3i)"=>"22", "role"=>"admin"}, "commit"=>"Create User"} 
Completed 500 Internal Server Error in 100ms 

ActiveRecord::MultiparameterAssignmentErrors (1 error(s) on assignment of multiparameter attributes): 
    app/controllers/users_controller.rb:18:in `new' 
    app/controllers/users_controller.rb:18:in `create' 
+0

什么是完整的错误? – sevenseacat 2013-03-22 14:40:36

+0

刚刚编辑后的开发日志错误 – pwz2000 2013-03-22 14:48:33

+0

什么数据类型是数据库中的“生日”列? – ramblex 2013-03-22 15:00:19

回答

2

实际的问题是数据库中的值是VARCHAR,而应该是DATE。现在它工作正常。

1

首先确保该生日的类型Date

这里在参数中,生日的值如下所示。

"birthday(1i)"=>"1995", "birthday(2i)"=>"3", "birthday(3i)"=>"22" 

但场ID 日期的类型。所以我认为,在更新生日字段之前,我们需要生成相应的日期对象,然后需要更新对象。

@user.birthday = Date.strptime("#{params['birthday(3i)']}/#{params['birthday(2i)']}/#{params['birthday(1i)']}", "%d/%m/%y") 

现在保存对象,并希望这次不会产生任何错误。

如果仍然有错误,请让我知道。