Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a field from params[:something]

People also ask

How to remove params from url js?

Just pass in the param you want to remove from the URL and the original URL value, and the function will strip it out for you. To use it, simply do something like this: var originalURL = "http://yourewebsite.com?id=10&color_id=1"; var alteredURL = removeParam("color_id", originalURL);

How do you remove parameters?

In the editor, select the parameters in the parameter line and choose Delete Parameters in the toolbar. The Delete and Adapt Parameters window is displayed, containing a list of parameters from which you can select the parameters you want to delete.

What is actioncontroller?

Action Controller is the C in MVC. After the router has determined which controller to use for a request, the controller is responsible for making sense of the request and producing the appropriate output.

What are strong parameters in Rails?

Strong Parameters, aka Strong Params, are used in many Rails applications to increase the security of data sent through forms. Strong Params allow developers to specify in the controller which parameters are accepted and used.


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


respond_to do |format|
  if params[:company].present?
    format.html { redirect_to(:controller => :shopping, :action => :index) }
  else
    format.html
  end
end

this will remove params from the url