質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

2095閲覧

rails プロフィール画像が更新できない

gsbzn_

総合スコア4

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/05/19 14:11

編集2022/01/12 10:55

前提・実現したいこと

dockerを用いてSNS、webアプリを作成中です。
プロフィール画像を変更したくても、できないのでそこを改善したい。

該当のソースコード

#profiles_controller class ProfilesController < ApplicationController before_action :authenticate_user! before_action :find_profile, only: [:show, :edit, :update] def show @profile = Profile.find(params[:id]) end def new return redirect_to edit_profile_path(current_user.profile) if current_user.profile.present? @profile = Profile.new end def edit end def create @profile = Profile.new(profile_params) @profile.user = current_user if @profile.save redirect_to root_path, notice: "プロフィール情報の登録が完了しました" else render :new end end def update if @profile.update(profile_params) redirect_to root_path, notice: "プロフィール情報の更新が完了しました" else render :edit end end def destroy end private def find_profile @profile = Profile.find(params[:id]) end def profile_params params.require(:profile).permit( :name, :learning_history, :purpose, :image ) end end

index.html.erb

<div class="content-wrapper"> <div class="content-block"> <% @posts.each do |post| %> <div class="content"> <div class="user-about"> <div class="image"> <% if post.user.image.attached? %> <%= image_tag post.user.image %> <% else %> <%= image_tag "nouser.png" %> <% end %> </div> <div class="profile"> <div class="name-history"> <div class="name"> <%= post.user.name %> </div> <div class="learning-history"> <%= "学習歴: #{post.user.learning_history}年" %> </div> </div> <div class="purpose"> <%= "プログラミングを勉強している目的: #{post.user.purpose}" %> </div> </div> </div>
<div class="text"> <p><%= post.content %></p> </div> <% if post.images.attached? %> <div class="images"> <% post.images.each do |image| %> <%= image_tag image %> <% end %> </div> <% end %> <div class="action-menu"> <div class="like"> </div> <div class="comment"> </div> </div> </div> <% end %>
</div> <div class="sidebar"> <div class="box"></div> <div class="box"></div> </div> </div>
#profile.rb class Profile < ApplicationRecord has_one_attached :image belongs_to :user validates :name, presence: true validates :learning_history, presence: true validates :purpose, presence: true end
#Profile_controller.rb class ProfilesController < ApplicationController before_action :authenticate_user! before_action :find_profile, only: [:show, :edit, :update] def show @profile = Profile.find(params[:id]) end def new return redirect_to edit_profile_path(current_user.profile) if current_user.profile.present? @profile = Profile.new end def edit end def create @profile = Profile.new(profile_params) @profile.user = current_user if @profile.save redirect_to root_path, notice: "プロフィール情報の登録が完了しました" else render :new end end def update if @profile.update(profile_params) redirect_to root_path, notice: "プロフィール情報の更新が完了しました" else render :edit end end def destroy end private def find_profile @profile = Profile.find(params[:id]) end def profile_params params.require(:profile).permit( :name, :learning_history, :purpose, :image ) end end
#_form.html.erb <%= form_with model: @profile, local: true do |f| %> <div class="form-group"> <%= f.label :name, "ニックネーム" %> <%= f.text_field :name %> </div> <div class="form-group"> <%= f.label :learning_history, "学習歴(年)" %> <%= f.number_field :learning_history %> </div> <div class="form-group"> <%= f.label :purpose, "学習目的" %> <%= f.text_field :purpose %> </div> <div class="form-group"> <%= f.label :image, "プロフィール画像"%> <%= f.file_field :images %><br> </div> <%# f.object = post %> <% if f.object.image.attached? %> <div class="image-preview"> <span>現在アップロードされている画像:</span> <%= image_tag f.object.image %> </div> <% end %> <div class="submit-block"> <%= f.submit 'Update Profile', class: "button" %> </div> <% end %>

詳細

画像を任意のものに変更しようとしても、
_form.html.erbのif文のプロフィール写真がない時の表示する画像が表示されてしまいます。
dbにそもそも反映されているのかも怪しいです。。再起動はしたのですが改善されませんでした。
乱雑な文章申し訳ありません。どなたかご教授いただけないでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

viewでは
<% if post.images.attached? %> とか <% post.images.each do |image| %> とか 複数imageがあるように見えて
controllerでは params.require(:profile).permit(:image ) と、一つしかないよ言ってます。どちらがただしい?

投稿2021/05/23 00:02

winterboum

総合スコア23284

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gsbzn_

2021/05/27 04:09

ご返信ありがとうございます。 viewが正しいと思うのですが。。。 user.rbでdelegate :image を定義して <%= image_tag post.user.image %> このコードでプロフィールが画像取れると思っているのですが 何か邪魔しているということでしょうか。。
winterboum

2021/05/27 08:17

ああそっちの方か。複数ヶ所あるのですから、どこなのか明記しないと正しい答えは得られません。 user.rb載せてください
gsbzn_

2021/05/27 12:43

失礼しました。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_one :profile, dependent: :destroy has_many :posts, dependent: :destroy delegate :name, :learning_history, :purpose, :image, to: :profile end 何卒よろしくお願い致します。
winterboum

2021/05/27 22:37

codeは<code>使って質問の方に書いてほしい。読みにくい。 質問読み直していて気がついたのですが、そもそもprofileに画像がsaveされているのか、を確認してますか?
winterboum

2021/05/27 22:42

そか 質問のtitleからして save出来ていない、という認識ですね? 載っているcodeが表示系だけなので、道外しました。 save update系のcode載せてください。
gsbzn_

2021/05/28 05:35

ご返信ありがとうございます。 profile.rbとprofiles_controller.rb追加しました。 もしかしたらコード足りてないかもです。。 何卒よろしくお願いします。
winterboum

2021/05/28 06:08

codeは<code>使って書いてほしい。読みにくい。
winterboum

2021/05/28 06:09

notice: "プロフィール情報の更新が完了しました" はでましたか?
gsbzn_

2021/05/28 15:56

失礼しました。 >>notice: "プロフィール情報の更新が完了しました" はでましたか? プロフィール更新後、表示されます。 しかし、<code>で追記させてもらった_form.html.erbの<span>現在アップロードされている画像:</span> という箇所がそもそも表示されません。
winterboum

2021/05/29 00:21

imge取り込みcodeに問題あり、ですね。 viewでは f.file_field :images profile_params では :image なので images で渡ってきたfile情報は無視されてます。
gsbzn_

2021/05/29 03:55 編集

ご返信ありがとうございます。 取り込みに問題ありなのですね。 profile_params を:images に変更で解決かと思ったのですがエラーになります。 コントローラーを修正で解決すると思っているのですがそうではないのでしょうか。。
winterboum

2021/05/29 08:04

逆、vuewを治す。 modelでの提議をみて
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問