[Rails carrier wave] How to not transition to the error screen even if the image upload is not selected

Solutions

Allow params to be nil by using fetch method instead of require.

private

  def food_record_params
    params.fetch(:food_record, {}).permit(:image)
  end

Technology used

Implemented image upload function using carrierwave.

gem 'carrierwave'

How to use carrierwave https://qiita.com/uchida0331/items/f99aba46fd1a6df9e753

problem

On the image upload screen, if you press the registration button while the image is not selected, an event that transitions to the error screen occurs. スクリーンショット 2020-12-18 0.28.01.png

スクリーンショット 2020-12-18 0.26.10.png

Cause

As shown in the error log, if the parameter does not contain a value in the require method, an error will occur. This is because require is a method for throwing an exception when params does not contain a value.

Reference material

ruby2.7 reference https://docs.ruby-lang.org/ja/latest/method/Hash/i/fetch.html

Recommended Posts

[Rails carrier wave] How to not transition to the error screen even if the image upload is not selected
[Error] How to resolve the event that the screen does not transition after editing
[Rails] How to prevent screen transition
How to solve the problem that the website image is not displayed after deploying to heroku on Rails 5
What to do if the background image is not applied after deployment
[Rails] About the error that the image is not displayed in the production environment
How to resolve the "OpenSSH keys only supported if ED25519 is available" error
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
[rails devise] How to transition users who are not logged in to the login page
How to move to the details screen by clicking the image
[Rails] How to display an image in the view
How to solve the problem when the value is not sent when the form is disabled in rails and sent
How to transition from the [Swift5] app to the iPhone settings screen
How to validate subsequent tests even if the test fails
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do if the app is not created with the latest Rails version installed when rails new
[Ubuntu 20.04] What to do if the external monitor is not recognized
[Rails] How to get the URL of the transition source and redirect
[Rails] What to do if data is not registered in DB
I tried to understand how the rails method "redirect_to" is defined
What to do if Cloud9 is full in the Rails tutorial
I tried to understand how the rails method "link_to" is defined
Isn't it reflected even if the content is updated in Rails?
[JQuery] How to preview the selected image immediately + Add image posting gem
[Rails 5] How to display the password change screen when using devise
How to write the view when Vue is introduced in Rails?
[Rails] How to solve the problem that the default image is overwritten when editing without uploading the image [Active Storage]
What to do if the image posted by refile disappears after setting a 404 error page in Rails
Rails Direct hit prohibition method (Do not transition to another person's edit screen by entering the URL)
What to do if the prefix c is not bound in JSP
[Rails] About the error when displaying the screen due to the autofocus of the form
[Docker] How to solve the error function not implemented @ io_fread ~ [Super easy]
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
How to constrain the action of the transition destination when not logged in
What to do if you get an "A server is already running." Error when you try to start the rails server
[Rails] How to upload images using Carrierwave
How to put out the error bundling
[Rails] How to use the map method
Upload Rails app image file to S3
[Rails] How to display error messages individually
How the website is displayed on the screen
How to pass the value to another screen
How to implement image posting using rails
[Rails] How to read the XML file uploaded from the screen with Hash type
If the parameter is an array, how to include it in Stopara's params.permit
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
Setting method that the size does not change even if CSS is changed
[Swift] What to do if the app icon is set but not reflected
How to set when "The constructor Empty () is not visible" occurs in junit
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)