Home>

I am creating a blue sale that allows multiple images to be posted on Rails.
HereThe src column of the image table becomes nul as shown in
There is a problem when implementing image display.

Corresponding source code

Since the column is nul, I assumed that there was a description error in the controller.

note_controller.rb

class NotesController</pre>
<strong>What I tried</strong>
<p>I wrote binding.pry in create and verified it.</p>
<pre><code data-language = "console">[1] pry (#<NotesController>)>params
=>"✓", "authenticity_token" =>"YwemLK5Z8ieHZY5t1aBzBxSaNyspwQmcEfwNwmm/RPsElu9vf6TYJWkzV3W38K0Mac5PuwPeUsvM" "," images_attributes "=>[{" image_url "=># , @original_filename="Posted sample 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" =>"post", "controller" =>"notes", "action" =>"create"} permitted: false>
[2] pry (#<NotesController>)>@ note.image
NoMethodError: undefined method `image'for #<Note: 0x00007ff5d9d88a48>
Did you mean? images
               images =
from /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'
[3] pry (#<NotesController>)>@ note.images
=>[#<Image: 0x00007ff5df37ba40 id: nil, src: nil, note_id: nil, created_at: nil, updated_at: nil>]
[4] pry (#<NotesController>)>@note
=>#<Note: 0x00007ff5d9d88a48 id: nil, title: "test", user_id: 1, status: "4", subject: "3", text: "test", created_at: nil, updated_at: nil>

Image_url can be confirmed in params, but it is nil in @ note.images
I'm still not sure if it's saved properly or not.
I would appreciate it if you could teach me how to solve it.

Supplementary information (FW/tool version, etc.)

Description of model and db.

models/note.rb

class Note</pre>
<p><br />
models.image.rb</p>
<pre><code data-language = "Ruby">class Image</pre>
<pre><code>ActiveRecord :: Schema.define (version: 2020_11_11_000852) do
  create_table "images", options: "ENGINE = InnoDB DEFAULT CHARSET = utf8", force :: cascade do | t |
    t.string "src"
    t.integer "note_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
  create_table "notes", options: "ENGINE = InnoDB DEFAULT CHARSET = utf8", force :: cascade do | t |
    t.string "title"
    t.integer "user_id"
    t.string "status"
    t.string "subject"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
(Omitted)
end