我最近执行了迁移,将string
列转换为Postgres数组。迁移如下:
def change
change_column :projects, :layout_slug, "varchar[] USING (string_to_array(layout_slug, ','))"
end
现在,我遇到了Unpermitted parameters: layout_slug
的问题。我已经尝试了其他StackOverflow问题的所有已接受的答案,但他们仍然无法为我工作。以下是我迄今为止所尝试的所有变体:
def project_params
params.require(:project).permit(:image_slug, { layout_slug: [] }, :category, ..., :selection => [:inputs => Project::ALLOWED_INPUTS.keys])
end
def project_params
params.require(:project).permit(:image_slug, { :layout_slug => [] }, :category, ..., :selection => [:inputs => Project::ALLOWED_INPUTS.keys])
end
然后我尝试删除:layout_slug => []
周围的括号,我收到了一个错误:
SyntaxError (/.../app/controllers/api/v1/projects_controller.rb:62: syntax error, unexpected ',', expecting =>
... :layout_slug => [], :category, :subcategory, :version, :ema...
... ^
/.../app/controllers/api/v1/projects_controller.rb:62: syntax error, unexpected ')', expecting keyword_end
/.../app/controllers/api/v1/projects_controller.rb:165: syntax error, unexpected end-of-input, expecting keyword_end):
# THIS ONE THROWS A SYNTAX ERROR
def project_params
params.require(:project).permit(:image_slug, :layout_slug => [], :category, :subcategory, :version, :email, :zip_code, :selection => [:inputs => Project::ALLOWED_INPUTS.keys])
end
然后我将layout_slug
移到了最后,然后又重新抛出了Unpermitted parameters: layout_slug
错误。
# This throws the same Unpermitted parameters error as before
def project_params
params.require(:project).permit(:image_slug, ..., :selection => [:inputs => Project::ALLOWED_INPUTS.keys], :layout_slug => [])
end
我已经对拼写进行了双重和三重检查,并且我已经验证了前端正在提交数组。
我错过了什么?我的迁移有问题吗?或者我的许可方法有什么明显的错误吗?
答案 0 :(得分:2)
已经测试了您的示例代码,似乎没问题:
raw_parameters = { :image_slug => "some_slug", :category => "A", :layout_slug => ["a", "b"] }
parameters = ActionController::Parameters.new(raw_parameters)
parameters.permit(:image_slug, :category, :layout_slug => [])
# {"image_slug"=>"some_slug", "category"=>"A", "layout_slug"=>["a", "b"]}
检查您的参数:layout_slug
格式,也许就是