0

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?

1 Answer 1

3

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.