pwz2000
pwz2000

Reputation: 1395

multiparameter attributes error for date_select

I added just this one piece of coding to app and now it doesn't allow anyone to register. I receive the following error: 1 error(s) on assignment of multiparameter attributes. The title of the error page is "ActiveRecord::MultiparameterAssignmentErrors in UsersController#create". I am not sure how to fix as other posts were not helpful unfortunately. Anyone have some solutions?

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'

Upvotes: 0

Views: 1583

Answers (2)

pwz2000
pwz2000

Reputation: 1395

The actual problem was the value in database was on VARCHAR, and instead it should be DATE. Now it works fine.

Upvotes: 2

Sagar Bommidi
Sagar Bommidi

Reputation: 1409

First make sure that the type of the birthday should be Date.

Here in parameters, the values for birthday is going like below.

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

But the type of the field id Date. So as i think, before updating the birthday field, we need to generate the appropriate date object and then need to update the object.

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

Now save the object and hopefully it will not raise any error this time.

If still having error, pls let me know.

Upvotes: 1

Related Questions