to_model委托给附件,但附件为nil

时间:2019-09-20 23:55:04

标签: ruby-on-rails ruby

所以我已经在这里问了这个问题: previous question 并没有得到太多帮助,我在寻找其他与我类似的问题,但没有解决方法。因此,我将再次询问该问题,以期寻求帮助。 问题是为用户个人资料横幅上传的图像没有显示在用户显示页面(也称为个人资料页面)上。我正在使用活动存储。我已经有了

  

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

error msg image

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

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)

2 个答案:

答案 0 :(得分:1)

已解决的问题:

  # 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并成功了!

答案 1 :(得分:0)

您似乎没有该用户的横幅图像。如果由于用户没有横幅而导致视图布局中断,请在代码中使用条件if

image_tag url_for(@user.banner_image) if @user.banner_image.present?

或者您可能想要一个后备图片来保留布局

image_tag url_for(@user.banner_image || '/public/fallback_banner_image.jpg')

上面的例子是一个例子,但是您需要包含一个实际的文件图像/路径