[RUBY] What to do if validation doesn't work with the update action

environment

What you want to implement

Reflect validation in the ʻupdate action of controller.rb`.

Cause

It is written from @ post.valid? in the if statement of error handling of the update action. With this writing method, validation will be effective for information that has already been registered.

post_controller.rb


def update
    if @post.valid?
      @post.update(post_params)
      flash[:notice] = "Editing is complete"
      redirect_to post_path(@post.id)
    else
      flash.now[:alert] = "Editing failed"
      render :edit
    end
  end

Solution

By starting writing from @ post.update (post_params), validation will work normally even with updates.

post_controller.rb


def update
    if @post.update(post_params)
      flash[:notice] = "Editing is complete"
      redirect_to post_path(@post.id)
    else
      flash.now[:alert] = "Editing failed"
      render :edit
    end
  end

Recommended Posts

What to do if validation doesn't work with the update action
What to do if TextToSpeech doesn't work on Android 11
What to do if the JSONHint annotation does not work with lombok and JSONIC
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if the server tomcat dies
What to do if the debug gem installation fails
What to do if the Rails server can't start
What to do if the rails server doesn't run out on AWS cloud9
What to do if the update does not take effect after deploying Rails AWS
What to do if you cannot execute with the command "Java package name / class name"
What to do if the adb command cannot be executed
What to do if you can't use the rails command
What to do if you change the Listen Address from the settings screen with Mattermost docker
What to do if you installed Ruby with rbenv but the version does not change
[Rails] What to do if you can't get an error message with the errors method
[Ubuntu 20.04] What to do if the external monitor is not recognized
What to do if Cloud9 is full in the Rails tutorial
What to do if the Eclipse Maven dependency Jar is wrong
What to do if you get angry with OpenSSL with pyenv install
What to do if you forget the root password in CentOS7
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
[Rails] What to do if you can't get parameters with form_with
What to do if you install Ubuntu
What to do if the app is not created with the latest Rails version installed when rails new
What to do if JavaMail cannot be used with OpenJDK 11 or later
[IOS] What to do when the image is filled with one color
What to do if the background image is not applied after deployment
What to do if the prefix c is not bound in JSP
What to do if you can't install the plugin from the Eclipse marketplace
What to do if you don't like the code generated by swagger-codegen-cli
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
What to do if you push incorrect information
What to do if mvn archetype: generate fails
What to do if you can't activate the select box created by bootstrap-select
What to do if you get the error message unrecognized selector send to instance "***"
What to do if the changes are not reflected in the jar manifest file
[Swift] What to do if the app icon is set but not reflected
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
What to do if you have enabled Use the WSL2 based engine in Docker Desktop with insufficient WSL2 installation
How to change the action with multiple submit buttons
What to do if you hit the wall of Too many symbol files when uploading with App Store Connect
What to do when The SSL certificate has expired
Things to check when it doesn't work with proguard
What to do if ClassNotFoundException occurs when starting Tomcat
What to do if you can't get the text of an element in Selenium
What to do if rails server can't be stopped
What should I do to reload the updated Dockerfile?
What to do if you accidentally create a model
What to do if an ActionController :: UnknownFormat error occurs
What to do if you get the error Couldn't find Item without an ID
What to do when routing settings do not work after building Docker environment with Laravel
What to do if password authentication fails in Docker/Postgres
What to do if Android Studio says "your CPU doesn't support vt-x or svm"
What to do if the breakpoint is shaded and does not stop during debugging
[JMH] What to do if the JMH Gradle Plugin says FAILURE: Build failed with an exception. A failure occurred while executing me.champeau.gradle.IsolatedRunner [Gradle]
[Java] [Spring] What to do if you cannot Autowire with Type Mismatch after annotating Spring Security
What to do if you don't see the test code error message in the terminal console
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[Rails] What to do when the view collapses when a message is displayed with the errors method