How to remove a field from params[:something]

Rails 4/5 - edited answer (see comments)

Since this question was written newer versions of Rails have added the extract! and except eg:

new_params = params.except[the one I wish to remove]

This is a safer way to 'grab' all the params you need into a copy WITHOUT destroying the original passed in params (which is NOT a good thing to do as it will make debugging and maintenance of your code very hard over time).

Or you could just pass directly without copying eg:

@person.update(params[:person].except(:admin))

The extract! (has the ! bang operator) will modify the original so use with more care!

Original Answer

You can remove a key/value pair from a Hash using Hash#delete:

params.delete :company

If it's contained in params[:user], then you'd use this:

params[:user].delete :company

You should probably be using hash.except

class MyController < ApplicationController
  def explore_session_params
    params[:explore_session].except(:account_id, :creator)
  end
end

It accomplishes 2 things: allows you to exclude more than 1 key at a time, and doesn't modify the original hash.


The correct way to achieve this is using strong_params

class UsersController < ApplicationController
  def create
    @user = User.new(user_params)
  end

  private

  def user_params
    params.require(:user).permit(:name, :age)
  end
end

This way you have more control over which params should be passed to model