[GO] How to reflect validation error and input contents on the previous page without using render in the action

Introduction

If you get stuck in validation when submitting form content Render the new post screen with the render method There should be many people who learned by displaying the error or the content that was entered.

But what if you can't use the render method? Actually, I'm using the Google Map API for my portfolio. When rendering a page with render, there is a problem that MAP is not displayed for some reason ...

It was a problem that could be solved by changing the specifications, but I didn't want to change it. I will show you how to achieve the above movement without using render.

Normally when using render

As an example in tasks_controller.rb The action new on the new post page and Suppose you have an action create that saves new posts.

tasks_controller.rb



def create
  @task = Task.new(task_params)
  if @task.save
    redirect_to @task, notice: 'You have saved the task.'
  else
    render :new
  end
end

@ task.save fails and render: new under ʻelseis executed, The new post page will be rendered. After that, as you already know, it feels like using@ task.errors.full_messages` in the view.

When not using render

It takes the form of re-accessing with redirect_to Error statements and input contents are stored in flash in the action.

tasks_controller.rb



def create
  @task = Task.new(task_params)
  if @task.save
    redirect_to @task, notice: 'You have saved the task.'
  else
    flash[:error_msgs] = @task.errors.full_messages
    flash[:tmp_body] = @task.body
    redirect_to new_task_url
  end
end

An error statement in flash [: error_msgs] The text contents are stored in flash [: tmp_body].

After that, it is OK if you use the value of flash in the access destination view. that's all!

Recommended Posts

How to reflect validation error and input contents on the previous page without using render in the action
How to copy and paste the contents of a sheet in Google Spreadsheet in JSON format (using Google Colab)
How to right click using keyboard input in RPA?
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to generate a query using the IN operator in Django
How to get all the keys and values in the dictionary
Notes on how to use marshmallow in the schema library
How to copy and paste command line content without mouse in bash on Linux or mac
How to return the data contained in django model in json format and map it on leaflet
How to intentionally issue an error in the shell During testing
How to install OpenCV on Cloud9 and run it in Python
How to give and what the constraints option in scipy.optimize.minimize is
How to get followers and followers from python using the Mastodon API
[EC2] How to install chrome and the contents of each command
How to judge that the cross key is input in Python3
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to use the render function defined in .mako (.html) directly in mako
How to display bytes in the same way in Java and Python
How to input a character string in Python and output it as it is or in the opposite direction.
How to reflect CSS in Django
How to divide and process a data frame using the groupby function
How to find out the number of CPUs without using the sar command
How to get the date and time difference in seconds with python
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
How to eliminate the conda activate error without annoying execution policy changes
TLE seemed to be scary depending on how the input was received