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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

562閲覧

【複数画像投稿】imageテーブルのsrcカラムがnulになってしまいます。

R_Hi-ra

総合スコア12

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/12 13:07

前提・実現したいこと

Railsで複数画像が投稿できる青売りを作成しています。
こちらのようにimageテーブルのsrcカラムがnulの状態になってしまい、
画像表示を実装する際に支障が出ます。

該当のソースコード

カラムがnulなのでコントローロに記述ミスがあるのかと仮定しました。

note_controller.rb

ruby

1class NotesController < ApplicationController 2 def index 3 @notes = Note.includes(:images).order('created_at DESC') 4 end 5 6 def new 7 @note = Note.new 8 @note.images.new 9 end 10 11 def create 12 @note = Note.new(note_params) 13 if @note.valid? 14 @note.save 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 private 22 23 def note_params 24 params.require(:note).permit(:title, :status, :subject, :text, images_attributes: [:src, :id]).merge(user_id: current_user.id) 25 end 26end

試したこと

binding.pryをcreateに記述して検証しました。

console

1[1] pry(#<NotesController>)> params 2=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"YwemLK5Z8ieHZY5t1aBzBxSaNyspwQmcEfwNwmm/RPsElu9vf6TYJWkzV3W38K0Mac5PuwPeUsvM+ZbXtxUYQA==", "note"=><ActionController::Parameters {"title"=>"test", "images_attributes"=>[{"image_url"=>#<ActionDispatch::Http::UploadedFile:0x00007ff5dfa2f378 @tempfile=#<Tempfile:/var/folders/3b/hl29yrr11_b92lydgcx3yt7c0000gn/T/RackMultipart20201112-91570-xx0qg9.jpg>, @original_filename="投稿サンプル2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"note[images_attributes][][image_url]\"; filename=\"\xE6\x8A\x95\xE7\xA8\xBF\xE3\x82\xB5\xE3\x83\xB3\xE3\x83\x95\xE3\x82\x9A\xE3\x83\xAB2.jpg\"\r\nContent-Type: image/jpeg\r\n">}], "status"=>"4", "subject"=>"3", "text"=>"test"} permitted: false>, "commit"=>"投稿する", "controller"=>"notes", "action"=>"create"} permitted: false> 3[2] pry(#<NotesController>)> @note.image 4NoMethodError: undefined method `image' for #<Note:0x00007ff5d9d88a48> 5Did you mean? images 6 images= 7from /Users/ryutarouhirasawa/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activemodel-5.2.4.4/lib/active_model/attribute_methods.rb:430:in `method_missing' 8[3] pry(#<NotesController>)> @note.images 9=> [#<Image:0x00007ff5df37ba40 id: nil, src: nil, note_id: nil, created_at: nil, updated_at: nil>] 10[4] pry(#<NotesController>)> @note 11=> #<Note:0x00007ff5d9d88a48 id: nil, title: "test", user_id: 1, status: "4", subject: "3", text: "test", created_at: nil, updated_at: nil> 12

paramsではimage_urlは確認できているのに、@note.imagesではnilになっており
きちんと保存ができているのか、できていないのか分からない状態のままです。
解消法などご教示いただけたらと思います。

補足情報(FW/ツールのバージョンなど)

modelとdbの記述です。

models/note.rb

ruby

1class Note < ApplicationRecord 2 belongs_to :user 3 has_many :images 4 accepts_nested_attributes_for :images, allow_destroy: true 5 validates :title, :status, :subject, :text ,presence: true 6 validates :images, presence: true 7end

models.image.rb

ruby

1class Image < ApplicationRecord 2 mount_uploader :src, ImageUploader 3 belongs_to :note 4end 5

db/schema.rb

1ActiveRecord::Schema.define(version: 2020_11_11_000852) do 2 3 create_table "images", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 4 t.string "src" 5 t.integer "note_id" 6 t.datetime "created_at", null: false 7 t.datetime "updated_at", null: false 8 end 9 10 create_table "notes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 11 t.string "title" 12 t.integer "user_id" 13 t.string "status" 14 t.string "subject" 15 t.text "text" 16 t.datetime "created_at", null: false 17 t.datetime "updated_at", null: false 18 end 19 20(中略) 21 22end

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

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

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

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

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

guest

回答1

0

自己解決

carrier waveからactive strageを用いることで解消できました。

投稿2020/11/25 22:51

R_Hi-ra

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問