to_model 委托给附件,但附件为零 [英] to_model delegated to attachment, but attachment is nil

查看:28
本文介绍了to_model 委托给附件,但附件为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经在这里问了这个问题:上一个问题并没有得到太多帮助,我寻找了与我类似的其他问题,但没有解决方案.所以我会再次问这个问题,希望得到帮助.问题是为用户配置文件横幅上传的图像未显示在用户显示页面(又名配置文件页面)上.我正在使用活动存储.我已经有了

So I asked this question already here: previous question and didn't get much help, I looked for other questions similar to mine, but no resolution. So I'll ask the question again in hopes of getting help. The issue is that the image that is uploaded for a user profile banner isnt showing on the user show page aka profile page. I'm using active storage. I already have the

has_on_attached :banner_image

has_on_attached :banner_image

.profile-header
  = image_tag url_for(@user.banner_image)

def set_user
  @user = User.find(params[:id])
end

设计注册配置:

def configure_account_update_params
  before_action :configure_account_update_params, only: [:update]
  devise_parameter_sanitizer.permit(:sign_up, keys: [:firstname, :banner_image])
end

错误信息图片

刷新个人资料页面时终端读取:

Terminal reads when refreshing the profile page:

Started GET "/users/1" for ::1 at 2019-09-20 21:51:59 -0700
Processing by UsersController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:11
  Rendering users/show.html.haml within layouts/application
  ActiveStorage::Attachment Load (0.2ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 1], ["record_type", "User"], ["name", "banner_image"], ["LIMIT", 1]]
  ↳ app/views/users/show.html.haml:2
  Listing Load (0.1ms)  SELECT "listings".* FROM "listings" WHERE "listings"."user_id" = ?  [["user_id", 1]]
  ↳ app/views/users/show.html.haml:5
  Rendered users/show.html.haml within layouts/application (5.5ms)
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/layouts/_navigation.html.haml:18
  Rendered layouts/_navigation.html.haml (8.7ms)
Completed 200 OK in 61ms (Views: 57.8ms | ActiveRecord: 0.7ms)

推荐答案

问题已解决:

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:firstname, :image])
  end

  # If you have extra params to permit, append them to the sanitizer.
  def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:firstname, :image])
  end

所以 configure_account_update_params 的定义被设置为允许 :signup 而不是 :account_update.我将其更改为 :account_update 并且成功了!

so the configure_account_update_params defenition was set to permit the :signup not :account_update. I changed it to :account_update and it worked!

这篇关于to_model 委托给附件,但附件为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆