[Ruby On Rails] In the create action and destroy action, emergency measures when redirect_to action:: show cannot be (cannot be used)

It is a memorandum. Thank you for browsing. Since it is a scribble, please point out any mistakes in the explanation or description.

background

I learned that you can call and execute an action in that controller by specifying the action :: action name in redirect_to.

Taking advantage of that learning, I was able to redirect the show action by writing redirect_to action:: show when it was updated (that is, when the edit was updated).

  def update
    if @hoge.update(hoge_params)
      redirect_to action: :show
    else
      render :edit
    end
  end

problem

However, in the create and destroy actions, redirect_to action:: show gave an error.

  def create
    @hoge = Hoge.new(hoge_params)
    if @hoge.save
      redirect_to action: :show
    else
      render :new
    end
  end

(Omitted)

  def destroy
    if @hoge.destroy
      redirect_to action: :show
    else
      render :show
    end
  end

Error statement

ActionController::UrlGenerationError in HogesController#create
No route matches {:action=>"show", :controller=>"hoges"}

Countermeasures

I searched rails routes for routes, described the Prefix path, and passed the key if necessary.

  def create
    @hoge = Hoge.new(hoge_params)
    if @hoge.save
      redirect_to hoge_path(current_user.id)
    else
      render :new
    end
  end

(Omitted)

  def destroy
    if @hoge.destroy
      redirect_to hoge_path(current_user.id)
    else
      render :show
    end
  end

By doing this, it was possible to transition to the same screen as show when creating a new one with the create action or deleting it with the destroy action.

* We believe that the above measures are just emergency workarounds. (Because I lack learning)

Things to learn in the future

To be able to understand the processing flow as to why and why the action :: show could not be redirected.

Recommended Posts

[Ruby On Rails] In the create action and destroy action, emergency measures when redirect_to action:: show cannot be (cannot be used)
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
Things to remember and concepts in the Ruby on Rails tutorial
Beginners create portfolio in Ruby on Rails
[Rails] "pry-rails" that can be used when saving with the create method
Cloud IDE: Heroku couldn't be installed in the Ruby on Rails tutorial
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
When the Ruby on Rails terminal rolls back
[Ruby on Rails] How to stop when Rails server cannot be stopped by Ctrl + C
[Rails] on :: Action Conditionally set callback cannot be avoided by using skip_callback and set_callback
[Ruby on Rails Tutorial] Error in the test in Chapter 3
(Ruby on Rails6) How to create models and tables
[Ruby on Rails] Quickly display the page title in the browser
[Ruby on Rails] Cookie is used and displayed only once at the first access (using jquery.cookie.js)
React.js CSS in Rails app deployed on EC2 cannot be loaded
"ClassNotFoundException" is displayed when tomcat is started in eclipse and cannot be started
A memorial service for the library used when competing in Ruby
(Ruby on Rails6) Creating a database and displaying it in a view
When log data accumulates in Rails and the environment stops working
Install Rails in the development environment and create a new application
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
I summarized the flow until implementing simple_calendar in Ruby on Rails.
[Ruby on Rails] Use the resources method to automatically create routes.
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
I ran the rails server command on windows10, Ruby2.6.6 and got "cannot load such file --sqlite3 / sqlite3_native"
Ruby on Rails Japanese-English support i18n
Measures when model cannot be created
Ruby methods often used in Rails
How to create a query using variables in GraphQL [Using Ruby on Rails]
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
What to do when Blocked Host: "host name" appears in Ruby on Rails
How to debug the processing in the Ruby on Rails model only on the console
Ruby on Rails When you don't know the cause of rollback when saving.
Check when the container cannot be accessed from Host in the Laravel on docker environment using VS Code's Remote container.
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
When creating an app with the new command in Ruby on Rails bundler: failed to load command: spring (/ Users/user name/directory name to create the app/app name/vendor/bundle/ruby ​​/ 2.6.0/bin/spring )