为什么我得到一个未定义的方法 `deep_merge' for ActionController::Parameters 错误?
Why am I getting an undefined method `deep_merge' for ActionController::Parameters error?
在我的 Rails 5 应用程序中,我试图在控制器中执行此操作:
def create
company_params = params.require(:company).permit(
:name,
:email,
:people_attributes => [
:first_name,
:last_name
]
).deep_merge(
:creator_id => current_user.id,
:people_attributes => [
:creator_id => current_user.id
]
)
@company = current_account.companies.build(company_params)
if @company.save
flash[:success] = "Company created."
redirect_to companies_path
else
render :new
end
end
出于某种原因,我收到此错误:
undefined method `deep_merge' for ActionController::Parameters:0x007fa24c39cfb0
我在这里错过了什么?
What am I missing here?
params
是 ActionController::Parameters
的实例。它不是哈希。不过,它有 一些 合并方法。
params.methods.grep(/merge/)
=> [:reverse_merge!, :reverse_merge, :merge!, :merge]
如果你想deep_merge
,以某种方式将你的参数转换为散列。
尽管在请求周期中修改参数不是一个好习惯,但如果您想这样做,总有一个 .to_hash.deep_merge
选项供您从参数实例中检索回哈希值。
好的,我无法让 deep_merge
工作,所以我最终这样做了:
def create
company_params = params.require(:company).permit(
:name,
:email,
:people_attributes => [
:first_name,
:last_name
]
)
@company = current_account.companies.build(company_params)
@company.creator = current_user
@person = @company.people.last
@person.account = current_account
@person.creator = current_user
if @company.save
flash[:success] = "Company created."
redirect_to companies_path
else
render :new
end
end
不确定这是好事还是坏事。感谢反馈!
在我的 Rails 5 应用程序中,我试图在控制器中执行此操作:
def create
company_params = params.require(:company).permit(
:name,
:email,
:people_attributes => [
:first_name,
:last_name
]
).deep_merge(
:creator_id => current_user.id,
:people_attributes => [
:creator_id => current_user.id
]
)
@company = current_account.companies.build(company_params)
if @company.save
flash[:success] = "Company created."
redirect_to companies_path
else
render :new
end
end
出于某种原因,我收到此错误:
undefined method `deep_merge' for ActionController::Parameters:0x007fa24c39cfb0
我在这里错过了什么?
What am I missing here?
params
是 ActionController::Parameters
的实例。它不是哈希。不过,它有 一些 合并方法。
params.methods.grep(/merge/)
=> [:reverse_merge!, :reverse_merge, :merge!, :merge]
如果你想deep_merge
,以某种方式将你的参数转换为散列。
尽管在请求周期中修改参数不是一个好习惯,但如果您想这样做,总有一个 .to_hash.deep_merge
选项供您从参数实例中检索回哈希值。
好的,我无法让 deep_merge
工作,所以我最终这样做了:
def create
company_params = params.require(:company).permit(
:name,
:email,
:people_attributes => [
:first_name,
:last_name
]
)
@company = current_account.companies.build(company_params)
@company.creator = current_user
@person = @company.people.last
@person.account = current_account
@person.creator = current_user
if @company.save
flash[:success] = "Company created."
redirect_to companies_path
else
render :new
end
end
不确定这是好事还是坏事。感谢反馈!