投稿サイトにて画像の保存ができていない解決方法について
解決したいこと
ユーザのプロフィール編集画面で名前、プロフィル画像、紹介文を追加、編集してユーザー詳細ページに遷移する際にエラーが出てしまうのを解決したいです。画像の投稿はActiveStrageを使っています。
プロフィール画像は画像が登録されていなけばnoimageの画像が表示されているようにしています。
発生している問題・エラー
ArgumentError
Can't resolve image into URL: to_model delegated to attachment, but attachment is nil
<%= @user.name %>
<%= image_tag @user.profile_image,size: "100x100" %>#ここでエラー
<%= @user.favorits.count %>
<%= link_to "フォロー数:", relationships_index_follow_path %>
<%= link_to "フォロワー数:", relationships_index_follower_path %>
該当するソースコード
*/CookOP/app/models/user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :dishes, dependent: :destroy
has_many :favorits, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :relationships, dependent: :destroy
has_one_attached :profile_image
def get_profile_image
unless profile_image.attached?
file_path = Rails.root.join('app/assets/images/noimage.png')
profile_image.attach(io: File.open(file_path), filename: 'default-image.jpg', content_type: 'image/jpeg')
end
end
end
*/CookOP/app/controllers/users_controller.rb
class UsersController < ApplicationController
def new
end
def show
@user = User.find(params[:id])
@profile_image = @user.profile_image
@dishes = @user.dishes.all
end
def edit
@user = User.find(params[:id])
end
def update
user = User.find(params[:id])
user.update(user_params)
redirect_to user_path(user.id)
end
def confirm
end
private
def user_params
params.require(:user).permit(:name, :profile_image, :profile)
end
end
*/CookOP/app/views/users/edit.html.erb
<h1>Users#edit</h1>
<%= form_with model: @user,url: user_path(@user.id), method: :patch do |f| %>
<%= f.label "名前" %>
<%= f.text_field :name %>
<%= f.label "プロフィール画像" %>
<%= f.file_field :profile_image %>
<%= f.label "プロフィール" %>
<%= f.text_area :profile %>
<%= f.submit "編集する" %>
<% end %>
<%= link_to "退会する", users_confirm_path %>
*/CookOP/app/views/users/show.html.erb
<h1>Users#show</h1>
<%= @user.name %>
<%= image_tag @user.profile_image,size: "100x100" %>
<%= @user.favorits.count %>
<%= link_to "フォロー数:", relationships_index_follow_path %>
<%= link_to "フォロワー数:", relationships_index_follower_path %>
<%= @user.profile %>
<% if @user.id == current_user.id %>
<%= link_to "編集する", edit_user_path(@user.id) %>
<% end %>
<h2>投稿記事一覧</h2>
<% @dishes.each do |dish| %>
<%= link_to dish_path(dish.id) do %>
<%= image_tag dish.dish_image, size: "100x100" %>
<% end %>
<!--お気に入り数-->
<%= dish.created_at %>
<%= dish.dish_name %>
<% end %>
自分で試したこと
エラーを翻訳したところ「画像をURLに解決できない:to_modelがattachmentに委ねられたが、attachmentはnilである。」となり添付ファイルが保存できていない?ような感じなのでコントローラのupdateアクションの記述を確認しましたが特に間違えている感じもなくストロングパラメータの記述も問題なさそうに感じました。
updateアクションでbinding.pryを使いuser,paramsの確認をして見ました。
userには画像らしき記述が無いようです。
paramsには"profile_image"=>#<ActionDispatch::Http::UploadedFile:0x00007f508df6a290という記述がありますがこの記述できちんと保存できているのかがわかりません。
解決方法を教えていただきたいです。
うまく説明のできていない部分もあると思いますがよろしくお願いします。
16: def update
17: user = User.find(params[:id])
18: binding.pry
=> 19: user.update(user_params)
20: redirect_to user_path(user.id)
21: end
1] pry(#<UsersController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"iDK1X6U8kyfJ4Fm+xTobHm7oj5Qtz7ci/a/jczhDX1ev3zkoUAxc7IeczqCulcGfx3VoJlSKrRLXgv62oloN3g==", "user"=>{"name"=>"太郎", "profile_image"=>#<ActionDispatch::Http::UploadedFile:0x00007f508df6a290 @tempfile=#<Tempfile:/tmp/RackMultipart20220412-16571-1c86bjs.webp>, @original_filename="犬jpg.webp", @content_type="image/webp", @headers="Content-Disposition: form-data; name=\"user[profile_image]\"; filename=\"\xE7\x8A\xACjpg.webp\"\r\nContent-Type: image/webp\r\n">, "profile"=>"太郎です"}, "commit"=>"編集する", "controller"=>"users", "action"=>"update", "id"=>"7"} permitted: false>
[2] pry(#<UsersController>)> user
=> #<User id: 7, email: "taro@gmail.com", created_at: "2022-04-12 01:36:23", updated_at: "2022-04-12 01:47:47", name: "太郎", profile: "太郎です", is_deleted: false>
[3] pry(#<UsersController>)> user_params
=> <ActionController::Parameters {"name"=>"太郎", "profile_image"=>#<ActionDispatch::Http::UploadedFile:0x00007f508df6a290 @tempfile=#<Tempfile:/tmp/RackMultipart20220412-16571-1c86bjs.webp>, @original_filename="犬jpg.webp", @content_type="image/webp", @headers="Content-Disposition: form-data; name=\"user[profile_image]\"; filename=\"\xE7\x8A\xACjpg.webp\"\r\nContent-Type: image/webp\r\n">, "profile"=>"太郎です"} permitted: true>