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

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

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

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

Q&A

解決済

1回答

308閲覧

Railsアプリで投稿した画像がデータベースに記録されない

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2020/03/29 23:50

前提・実現したいこと

Railsアプリで投稿した画像をデータベースに記録させたいです。
https://github.com/mkakiz/tadakashi_app

発生している問題

以下のコントローラで投稿を行っています。

(posts_controller.rb) def create @post = Post.new( content: params[:content], user_id: @current_user.id ) if @post.save #DB保存して、@post.id発行 if params[:image] @post.post_image_name = "#{@post.id}.jpg" image = params[:image] File.binwrite("public/post_images/#{@post.post_image_name}", image.read) end flash[:notice] = "投稿を作成しました" redirect_to("/posts/index") else render("posts/new") end end

実行後にRails Consoleで投稿を見ると、投稿画像のカラムがnilとなっています。

<Post id: 9, content: "cat pic", created_at: "2020-03-29 23:37:23", updated_at: "2020-03-29 23:37:23", user_id: 1, post_image_name: nil>]>

ユーザー画像をアップデートするときも同じように書いてますが、こちらは正常に動きます。

def update @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 if @user.save flash[:notice] = "ユーザー情報を編集しました" redirect_to("/users/#{@user.id}") else render("users/edit") end end

アプリに画像が保存はされているようです。

app/public/post_images/9.jpg

データベースは以下のように設定されています。

create_table "posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id", null: false t.string "post_image_name" end

postsテーブルのpost_image_nameに記録されると思っているのですが、上手くいってません。

すみませんがアドバイスを頂けないでしょうか。
よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

@user.image_name = "#{@user.id}.jpg"

の処理の後にsaveされているかの差です。
createではされていませんが、updateではされています!

ID発行してからファイル名を付けたいのであれば、もう一度save投げる感じでしょうか

controller

1def create 2 @post = Post.new( 3 content: params[:content], 4 user_id: @current_user.id 5 ) 6 7 if @post.save #DB保存して、@post.id発行 8 if params[:image] 9 @post.post_image_name = "#{@post.id}.jpg" 10 image = params[:image] 11 File.binwrite("public/post_images/#{@post.post_image_name}", image.read) 12 @post.save # ←追加 本来はsaveできなかった処理も考える 13 end 14 flash[:notice] = "投稿を作成しました" 15 redirect_to("/posts/index") 16 else 17 render("posts/new") 18 end 19 end

投稿2020/03/30 01:33

編集2020/03/30 02:18
ymneet

総合スコア154

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

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

退会済みユーザー

退会済みユーザー

2020/03/30 02:08

回答ありがとうございます。 if params[:image] @post.post_image_name = "#{@post.id}.jpg" image = params[:image] @post.save File.binwrite("public/post_images/#{@post.post_image_name}", image.read) end で保存されました。 ただこれだと画像がない投稿の後で画像がある投稿が保存される動作になります。 私の認識としては、 if @post.save if params[:image] @post.post_image_name = "#{@post.id}.jpg" image = params[:image] File.binwrite("public/post_images/#{@post.post_image_name}", image.read) end で、投稿を保存するときに画像があるなら、image変数に入れて保存する、という動きを想定していたのですが... 再度すみませんが、見ていただけると助かります。
ymneet

2020/03/30 02:14

if @post.save if params[:image] @post.post_image_name = "#{@post.id}.jpg" image = params[:image] File.binwrite("public/post_images/#{@post.post_image_name}", image.read) @post.save # ←追加 end flash[:notice] = "投稿を作成しました" . . . これでどうでしょうか? 2回目のsaveは、@post.post_image_nameだけをupdateかける動作になると思います!
退会済みユーザー

退会済みユーザー

2020/03/30 02:33

再度回答ありがとうございます。 希望の動作になりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問