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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

1回答

1221閲覧

プロフィール画像を表示させたい

akiakiakirin

総合スコア13

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2018/08/22 20:04

編集2018/08/23 10:03

プロフィール画像を表示させたいのですがなかなかうまく行きません。
イメージ説明←このようになってしまいます。

public/user_imagesには画像が保存されているのですがデータベースには保存されていないようです。

イメージ説明

イメージ説明

何回も試行錯誤を繰り返したのですが未だ解決に至りません。どなたかご教授いただけないでしょうか。


routes.rb

Rails.application.routes.draw do post "user/:id/update" => "home#update_user" get 'pay/payment' devise_for :users get "home/:id/likes" => "home#likes" post "likes/:post_id/create" => "likes#create" post "likes/:post_id/destroy" => "likes#destroy" root 'home#index' get "home/new_fc" => "home#new_fc" post "home/create" => "home#create" get "home/profile/:id" => "home#profile" get ":id/show_fc" => "home#show_fc" get ":id/edit_fc" => "home#edit_fc" post ":id/update_fc" => "home#update_fc" post ":id/fc/destroy" => "home#destroy" get "pay" => "pay#payment" end

home_controller.rb

class HomeController < ApplicationController def index @posts = Post.all.order(created_at: :desc) # @post = Post.find_by(id: params[:id]) # @user = @post.user @users = User.all.order(created_at: :desc) end def new_fc end def create @post = Post.find_by(id: current_user.id) @post = Post.new(content: params[:content], user_id: @current_user.id) if @post.save redirect_to("/") else render("new_fc") end end def show_fc @post = Post.find_by(id: params[:id]) @user = @post.user @likes_count = Like.where(post_id: @post.id).count end def edit_fc @post = Post.find_by(id: params[:id]) end def update_fc @post = Post.find_by(id: params[:id]) @post.content = params[:content] if @post.save flash[:notice] = "編集完了!!!" redirect_to("/#{@post.id}/show_fc") else render("edit_fc") end end def profile @user = User.find_by(id: params[:id]) @post = Post.find_by(id: params[:id]) @likes = Like.where(user_id: @user.id) end def update_user @user = User.find_by(id: params[:id]) @user.name = params[:name] @user.email = params[:email] # 画像を保存する処理を追加してください if params[:image] @user.image_name = "#{@user.id}.jpg" image = params[:image] File.binwrite("public/user_images/#{@user.image_name}",image.read ) end @user.save flash[:notice] = "ユーザー情報を編集しました" redirect_to("/home/profile/#{@user.id}") end def likes @user = User.find_by(id: params[:id]) @post = Post.find_by(id: params[:id]) @likes = Like.where(user_id: @user.id) end def destroy @post = Post.find_by(id: params[:id]) @post.destroy redirect_to("/") end end

edit.html.erb

<h2>編集</h2> <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= devise_error_messages! %> <div class="field"> <%= f.label :email , "メールアドレス" %><br /> <%= f.email_field :email, autofocus: true, autocomplete: "email" %> </div> <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div> <% end %> <div class="field"> <%= f.label :password , "パスワード"%> <i>(leave blank if you don't want to change it)</i><br /> <%= f.password_field :password, autocomplete: "off" %> <% if @minimum_password_length %> <br /> <em><%= @minimum_password_length %> characters minimum</em> <% end %> </div> <div class="field"> <%= f.label :password_confirmation , "確認パスワード" %><br /> <%= f.password_field :password_confirmation, autocomplete: "off" %> </div> <div class="field"> <%= f.label :current_password , "現在のパスワード" %> <i>(we need your current password to confirm your changes)</i><br /> <%= f.password_field :current_password, autocomplete: "off" %> </div> <div class="actions btn btn-size-small"> <%= f.submit "保存する" %> </div> <% end %> <%= form_tag("/user/#{@user.id}/update", {multipart: true}) do %> <p>画像</p> <input name="image" type="file"> <input type="submit" value="保存"> <% end %> <h3>アカウントを削除する</h3> <p>Unhappy? <%= button_to "削除する", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p> <%= link_to "戻る", :back %>

profile.html.erb

<h3>プロフィール</h3> <h2><%= @user.name %>(プロフィール画像)</h2> <p><%= @user.name %></p> <img src="<%= "/user_images/#{@user.image_name}" %>"> <table> <tr> <th>ファンクラブ総会員数</th> <th>入会ファンクラブ数</th> </tr> <tr> <td>11</td> <td>20</td> </tr> </table> <% if @user.id == current_user.id %> <p><%= @user.name %>さんが作ったファンクラブ</p> <% @user.posts.each do |post|%> <section class="card"> <div class="card-img-parent"> <img class="card-img" src="http://webcreatorbox.com/sample/images/bear.jpg" alt=""> <li class="card-img-child"><%= link_to(post.content, "/#{post.id}/show_fc") %></li> </div> <div class="card-content"> <p class="card-text">WebデザインやWebサイト制作、最新のWeb業界情報などを紹介していくサイト。</p> </div> </section> <% end %> <p><%= @user.name %>さんがいいねしたファンクラブ</p> <% @likes.each do |like|%> <% post = Post.find_by(id: like.post_id)%> <% user = User.find_by(id: like.user_id) %> <section class="card"> <div class="card-img-parent"> <img class="card-img" src="http://webcreatorbox.com/sample/images/bear.jpg" alt=""> <li class="card-img-child"><%= link_to(post.content, "/#{post.id}/show_fc") %></li> </div> <div class="card-content"> <h1 class="card-title"><%= link_to(post.user.name, "/home/profile/#{post.user.id}") %></h1> <p class="card-text">WebデザインやWebサイト制作、最新のWeb業界情報などを紹介していくサイト。</p> </div> </section> <% end%> <ul> <li> <%= link_to 'パスワード変更', edit_user_registration_path %> </li> <li> <%= link_to 'ファンクラブを作る', home_new_fc_path %> </li> </ul> <% else %> <p>ヤッホイ</p> <% end %>

models/user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :posts def posts return Post.where(user_id: self.id) end end

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

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

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

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

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

guest

回答1

0

プロフィール画像を表示しているのは、profile.html.erbの、

ruby

1<img src="<%= "/user_images/#{@user.image_name}" %>">

のところですよね。なので、Userクラスのimage_nameメソッドに問題があるのではないでしょうか。
/user_images/22.jpgのようなパスで画像が表示されるようなので、上記の部分で実行時に表示されるHTMLと比較しつつ、app/model/user.rbのimage_nameメソッドの実装を確認すると問題が分かるんではないかと思います。
(なお、データベースには画像独自の情報は特に保存されず、IDを元に画像のパスを生成する形になっているのではないかと推測します。なのでデータベースに問題はないかと。)

投稿2018/08/23 09:36

takahashim

総合スコア1877

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

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

akiakiakirin

2018/08/23 10:05

takahashimさま、ご回答ありがとうございます! ご指摘の通りapp/model/user.rbのimage_nameメソッドを確認したところ何も書かれていませんでした。 調べたのですが実装の仕方がわからない状態です。どうかご教授いただけないでしょうか。
takahashim

2018/08/24 12:12

何も書かれていないというか、image_nameカラムがあればそこの値が拾えるはずです。 よくよくみると、home_controller.rbのupdate_userメソッドが正しく動けばimage_name=メソッドが実行されて、正しい値が埋まると思います。こちらは正常に稼働しているでしょうか。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問