【问题标题】:Active Storage in Ruby on Rails 6Ruby on Rails 6 中的主动存储
【发布时间】:2021-04-12 00:11:40
【问题描述】:

我正在学习 Ruby on Rails,但我一直在使用 Active Storage 和上传文件。我正在使用本教程:

Tim DiSabatino

运行项目时,我在终端中看到错误:Unpermitted parameter: :header_image

我双重、三重检查了我的代码,在绝望中我什至通过 chmod -R 777 更改了整个项目目录中的权限,但无法消除此错误。

我正在 A2hosting.com 使用 Ubuntu 20.04 在远程非托管服务器上运行该项目

有什么想法吗?提前谢谢!

【问题讨论】:

  • 你必须在强参数中添加 header_image 像这样 def post_params params.require(:post).permit(:title, :body, :header_image) end

标签: ruby-on-rails rails-activestorage


【解决方案1】:

如有疑问,请参阅官方文档 非常清楚并且有代码示例 https://edgeguides.rubyonrails.org/active_storage_overview.html 在你必须定义的模型中

has_one_attached :header_image

在提交表单后附加文件

post.header_image.attach(params[:header_image])

控制器对同一方法有 2 个定义,并且只会采用 2 种可能性中的一种 添加以下从教程中编辑的内容

# /app/controllers/posts_controller.rb

def post_params
  params.require(:post).permit(:title, :body, files: [], :header_image)
end

header_images 必须添加到允许的参数列表中

【讨论】:

  • 谢谢!有效。我忽略了我在控制器中定义了两次 post_params。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多