user7055375
user7055375

Reputation:

ActiveModel::ForbiddenAttributesError when creating user

I’m using Rails 5 and Postgres 9.5. I’m having a trouble when submitting a form meant to create an user in my database. I have this in my controller

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "You signed up successfully"
      flash[:color]= "valid"
    else
      flash[:notice] = "Form is invalid"
      flash[:color]= "invalid"
    end
    render "new"
  end

and here’s how my table looks in my Postgres database …

sims=> \d users;
                                         Table "public.users"
       Column       |            Type             |                     Modifiers                      
--------------------+-----------------------------+----------------------------------------------------
 id                 | integer                     | not null default nextval('users_id_seq'::regclass)
 username           | character varying           | 
 email              | character varying           | 
 encrypted_password | character varying           | 
 salt               | character varying           | 
 first_name         | character varying           | 
 last_name          | character varying           | 
 created_at         | timestamp without time zone | not null
 updated_at         | timestamp without time zone | not null
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

But upon submitting my form with the below parameters, I get the error, as you can see from my logs …

Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"q4YmPjUhD5olnRRgCt/gUuCDrb0lt+EqOxpzXGdtGHBtkwPEYgyp12H8lF04FHpqrZRZCip1Mo8/tvGQinPpJg==", "user"=>{"username"=>”mysuername”, "email"=>”[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Signup"}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)



ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):

There is no other information in the logs. What else do I need to do to get my form to submit?

Upvotes: 0

Views: 4714

Answers (1)

Sean Huber
Sean Huber

Reputation: 3985

Rails is preventing the save because of security concerns. This guide has a nice, concise explanation: http://blog.teamtreehouse.com/rails-4-strong-paremeters

To fix your specific example, change the @user = User.new(params[:user]) line to:

@user = User.new(params.require(:user).permit(:username, :email, :password, :password_confirmation))

Upvotes: 3

Related Questions