[Rails] fields_for is not displayed

This time, when I tried to use "file_field" in "fields_for", it was not reflected in the view and I had a hard time, so I will describe how to solve it as a memorandum. I hope it will help those who have the same problems.

The model that appears this time

・ (Parent) post model ・ (Child) image model

We associate with post model and image model accepts_nested_attributes_for.

Assumption

Create SNS like Twitter and FB. I am about to create a post form using "form_for".

Sentence → post model Photo → image model I wanted to save it in, and created a view like this:

Wrong code

(Only the relevant parts are excerpted for easy understanding)

haml


.form__contents
  = form_for @post, id: "new_post" do |f|
    .title
      = f.text_field :title
    .text
      = f.text_area :text
    .image
      = f.fields_for :images do |i|
        = i.file_field :image
    = f.submit "Post!"

Alright, coding is complete! !! Let's go to the view! !! …that,,"= f.fields_for :images do |i|The following is not reflected ... Moreover, I don't know what's wrong because there are no errors ... I did as I checked ...

Code that worked

haml


.form__contents
  = form_for @post, id: "new_post" do |f|
    .title
      = f.text_field :title
    .text
      = f.text_area :text
    .image
      = f.fields_for :images, @post.images.build do |i|
        = i.file_field :image
    = f.submit "Post!"

Summary

I just thought that if you write the model you want to save in "fields_for", it will be saved in the column described in "file_field".

It seemed that I had to pass ** an instance of the child model ** to the second argument of "fields_for".

I see ... It's natural because I'm giving an instance to "form_for" as well. (In the first place, I knew for the first time that I could create an instance of images.build!)

More and more.

Referenced site

Recommended Posts

[Rails] fields_for is not displayed
rails AWS deployment is not reflected
Data is not registered in Rails.
[Rails] views file creation # text is displayed
MySQL is not running stably ... Rails app is not running.
Image is not displayed in production environment
[Rails] About the error that the image is not displayed in the production environment
The devise error message is not displayed properly.
GoogleMap is not displayed until the browser is reloaded
rails new app is not created with app name
When the project is not displayed in eclipse
Rails is Owakon www
Rails must be exist error (@save is not executed)
[Error] The app is not displayed in the production environment
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
DateFormat is not thread safe
"tx" is not bound error
What is Rails gem devise?
rails error Library not loaded
Ruby # {} is not variable expansion
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
What to do if FacesMessage is set but not displayed
Rails is difficult and painful!
Rails is difficult and painful! Ⅱ
When [command not found: composer] is displayed when [composer install] is displayed in Laravel
[Rails 6] jQuery introduction ・ uncaught reference error $ is not defined Error solution
[Rails] When the layout change of devise is not reflected
fields_for is used like this
[Java] final override error is not displayed at compile time
What is Rails Active Record?
How to solve the problem that the website image is not displayed after deploying to heroku on Rails 5
About the problem that the image is not displayed after AWS deployment
[Rails] What to do if data is not registered in DB
[rails] Edit of view page created by devise is not reflected
Uncaught ReferenceError: Vue is not defined
Ruby's attr_accessor is not an accessor
that? WebView (swift) is not working.
[Ruby on Rails] What is Bcrypt?
Add Not null constraint after Rails
Java Calendar is not a singleton.
Why Java Vector is not used
[Must-see for beginners] rails_12factor is not required to publish Rails app to Heroku
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
Google chrome is not maximized correctly on Ubuntu 16.04 (remote desktop is also not displayed correctly)
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error