Ruby-on-rails 如何从 params[:something] 中删除一个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5150676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:33:10  来源:igfitidea点击:

How to remove a field from params[:something]

ruby-on-rails

提问by providence

My registration form, which is a form for the Users model, takes a string value for company. However, I have just made a change such that users belongs_to companies. Therefore, I need to pass an object of Company to the Users model.

我的注册表单是用户模型的表单,它采用公司的字符串值。但是,我刚刚进行了更改,使用户属于公司。因此,我需要将 Company 的对象传递给 Users 模型。

I want to use the string value from the form to obtain the an object of Company:

我想使用表单中的字符串值来获取 Company 的对象:

@user.company = Company.find_by_name(params[:company])

I believe the above works, however the form is passing the :company (which is string) into the model when I call:

我相信上面的方法是有效的,但是当我调用时,表单将 :company (它是字符串)传递到模型中:

@user = User.new(params[:user])

Therefore, I want to know (and cannot find how) to remove the :company param before passing it to the User model.

因此,我想知道(并且找不到如何)在将 :company 参数传递给 User 模型之前删除它。

回答by Jeremy Ruten

Rails 4/5 - edited answer(see comments)

Rails 4/5 - 编辑的答案(见评论)

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

由于此问题是编写的,较新版本的 Rails 已添加了摘录!除外例如:

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!

extract!(有!砰运营商)将修改原来那么更为谨慎使用!

Original Answer

原答案

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

您可以使用Hash#delete以下方法从哈希中删除键/值对:

params.delete :company

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

如果它包含在 params[:user] 中,那么你可以使用这个:

params[:user].delete :company

回答by samuraisam

You should probably be using hash.except

您可能应该使用 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.

它完成两件事:允许您一次排除 1 个以上的键,并且不修改原始哈希。

回答by Deepak Mahakale

The correct way to achieve this is using strong_params

实现这一目标的正确方法是使用 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

这样您就可以更好地控制应将哪些参数传递给模型

回答by Taimoor Changaiz

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

这将从网址中删除参数

回答by Washington Botelho

To be possible to delete you can do a memo:

为了可以删除你可以做一个备忘录

def parameters
  @parameters ||= params.require(:root).permit(:foo, :bar)
end

Now you can do:

现在你可以这样做:

parameteres.delete(:bar)
parameters
=> <ActionController::Parameters {"foo" => "foo"} permitted: true>