date_select的多参数属性错误

时间:2013-03-22 14:38:01

标签: ruby-on-rails

我在应用程序中添加了这一段编码,现在它不允许任何人注册。我收到以下错误:1个错误分配多参数属性。错误页面的标题是“UsersController #create中的ActiveRecord :: MultiparameterAssignmentErrors”。我不知道如何解决,因为其他帖子不幸没有帮助。有人有解决方案吗?

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"=>"james@james.com", "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'

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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")

现在保存对象,希望这次不会引发任何错误。

如果仍然有错误,请告诉我。