gpt4 book ai didi

ruby-on-rails - "No explicit conversion of Symbol into String"用于 rails 4.0.1 中的新记录(仅限)

转载 作者:数据小太阳 更新时间:2023-10-29 06:53:26 24 4
gpt4 key购买 nike

在我的 Rails 4 升级之后,尝试为我的任何 ActiveRecord 类创建一个新记录

No explicit conversion of Symbol into String

例如,这里是我的链接的links_params方法

def link_params
params.require(:link)
.permit(:url_address, :group_id, :alt_text, :version_number,
:position, :content_date, :verified_date) # This is line 157
end

# line 118 is: @link = Link.new(link_params)

但是我明白了

TypeError (no implicit conversion of Symbol into String):
app/controllers/links_controller.rb:157:in `link_params'
app/controllers/links_controller.rb:118:in `create'

This error occurred while loading the following files:
link

Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"0FqFTx2EjCIO+R+rm97lF15+id4b452n+dBuUNxAL9U=",
"link"=>{"url_address"=>"http://www.google.com",
"alt_text"=>"",
"version_number"=>"",
"group_id"=>"49",
"content_date"=>"08/18/2014"},
"commit"=>"Save"}

最佳答案

我不确定它是怎么发生的,但看起来您的 params 对象只是一个散列...而不是 ActionController::Parameters 对象。当 params 只是一个散列时:

params = {"utf8"=>"✓", "authenticity_token"=>"0FqFTx2EjCIO+R+rm97lF15+id4b452n+dBuUNxAL9U=", "link"=>{"url_address"=>"http://www.google.com", "alt_text"=>"", "version_number"=>"", "group_id"=>"49", "content_date"=>"08/18/2014"}, "commit"=>"Save"}

params.require(:link)
=> TypeError: no implicit conversion of Symbol into String

params.class
=> Hash

但是如果它是一个 ActionController::Parameters 对象

params2 = ActionController::Parameters.new({"utf8"=>"✓", "authenticity_token"=>"0FqFTx2EjCIO+R+rm97lF15+id4b452n+dBuUNxAL9U=", "link"=>{"url_address"=>"http://www.google.com", "alt_text"=>"", "version_number"=>"", "group_id"=>"49", "content_date"=>"08/18/2014"}, "commit"=>"Save"})

params2.require(:link)
=>{"url_address"=>"http://www.google.com", "alt_text"=>"", "version_number"=>"", "group_id"=>"49", "content_date"=>"08/18/2014"}

params2.class
=>ActionController::Parameters

您是否在 link_params 掌握它之前对 params 做了一些事情?

编辑:根据API Docs , Strong params 仅在 Rails 4.0.2 中可用。如果您使用的是早期版本,则必须坚持使用 Rails 3 attr_accessible

关于ruby-on-rails - "No explicit conversion of Symbol into String"用于 rails 4.0.1 中的新记录(仅限),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25374559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com