[RAILS] Solution when the image data becomes nil via the confirmation screen

Is there a time when you want to create in the flow of posting screen → confirmation screen → posting completion? I just added a confirmation screen, but it got stuck because the data was saved and handled a little differently. .. I will write the experience at that time as a memorandum!

Those who will find this article helpful

-The image is displayed on the confirmation screen, but the image cannot be uploaded on the post details screen. -When you debug with binding.pry etc. and check the behavior of the post confirmation action (confirm action), only the image is nil. -Even if I check it by operating SQL, only the image data is not saved.

Why become nil?

Apparently, there is no image data in the information returned from the confirmation screen when going through the confirmation screen.

It seems that it is necessary to save the image data separately before displaying the confirmation screen, and take it out and use it.

Solution conclusion

-Add the description of hidden in the confirmation screen. -Use the : image_cache parameter. (Don't forget to pass : image_cache in the controller ...)

hidden = ** Used when sending hidden data to the server. The value specified in the value attribute is sent to the server. ** **

image_cache = ** Parameter that handles the image data itself. (This is a parameter used when the confirmation screen is mounted.) **

Reference code

new.html.erb


<div class="thumbnail">
  <%= f.file_field :image %>
  <%= f.hidden_field :image_cache %>← Addendum
</div>
<%= f.submit "To confirmation screen" %>

confirm.html.erb


<%= form_with model: @tournament, url: tournaments_path, html: { method: :post }, local: true do |f| %>
  <%= f.hidden_field :start_time %>
  <%= f.hidden_field :image %>
  <%= f.hidden_field :image_cache %>← Addendum
  <%= f.submit "create"%>
<% end %>

tournaments_controller.rb


def tournament_params
    params.require(:tournament).permit(:start_time, :image, :image_cache)
end

Now you can receive and display the image data! ..must.

Finally

The description when going through the confirmation screen changes from place to place, so it gets stuck unexpectedly. I want to experience more and grow! !!

I hope it helps those who are in the same situation. LGTM: thumbs up: I would be grateful if you could solve it or find it helpful. Lol

Recommended Posts

Solution when the image data becomes nil via the confirmation screen
When using FloatingPanel, the tableView created by Stroybard becomes nil
When you want to ZIP download the image data saved locally
Display an image on the base64 screen