[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show

About the article

It is a memorandum.

This is how to write redirect_to of the child controller when a parent-child relationship is created in routes.rb.

I used to redirect the create and update actions of nested child controllers to root_path.

Aim for de-root_path.

Premise

Parent: contents Child: descriptions

routes.rb


root to: 'contents#index'

resources :contents, only: [:index, :new, :create, :show, :edit, :update] do
    resources :descriptions, only: [:new, :create, :edit, :update, :destroy]

As mentioned above, it is assumed that: show is not specified for the child.

rails routes↓ スクリーンショット 2020-12-29 18.59.36.png

When specified by root_path

descriptions_controller


(Omitted)

  def new
    @description = Description.new
  end

  def create
    @description = Description.new(description_params)
    if @description.save
      redirect_to root_path #Here is
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @description.update(description_params)
      redirect_to root_path #Here is
    else
      render :edit
    end
  end

(Omitted)

In this way, you can jump to the contents # index of the nested parent by setting "redirect_to root_path".

However, it is inconvenient for users to jump to the top page after updating the information.

You have to bother to click from the top page to the updated information and go through several pages.

So I thought about de-root_path.

In case of de-root_path

descriptions_controller.rb


  def new
    @description = Description.new
  end

  def create
    @description = Description.new(description_params)
    if @description.save
      redirect_to content_path(@description.content_id) #Here is
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @description.update(description_params)
      redirect_to content_path(@description.content_id) #Here is
    else
      render :edit
    end
  end

I deleted the description of root_path and rewritten it as content_path (@ description.content_id). By writing like this, you can redirect from pages such as "/ contents/4/descriptions/new" and "/ contents/3/descriptions/edit" to "/ contents/1 /", which is the same behavior as the parent show. I can.

Way of thinking

Let's look at each one.

Check the routing again. スクリーンショット 2020-12-29 18.59.36.png

If you pay attention to the prefix, it says content, so write redirect_to content_path.

descriptions_controller.rb


(Omitted)

def create or def update
    @description = Description.new(description_params)
    if @description.save
      redirect_to content_path #Here is
    else

(Omitted)

As mentioned above, you can move up to "/ contents /" by writing "content_path". But it's still incomplete. It is necessary to describe which id you want to move to.

The points when specifying the id are the following two points.

  1. Check the params leading up to create and update
  2. Specify the id so that it becomes "/ contents/id /"

If you check the contents of the params that have been brought to create and update,

スクリーンショット 2020-12-29 19.26.07.png

You can see that the content_id is included. Therefore, you can specify the instance variable (@description) that has params, and then specify the "id" (content_id) in (.).

descriptions_controller.rb


(Omitted)

def create or def update
    @description = Description.new(description_params)
    if @description.save
      redirect_to content_path(@description.content_id) #Here is
    else

(Omitted)

Finally

I wrote that it is quite difficult to explain the flow of data in verbalization. .. I myself am a beginner in programming, so please point out any points that cannot be reached.

Reference article

https://stackoverflow.com/questions/20368717/rails-i-cant-pass-a-validation-error-in-a-redirect

Recommended Posts

[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show
Apply CSS to a specific View in Ruby on Rails
[Ruby On Rails] How to search and save the data of the parent table from the child table
[rails] How to display parent information in child view in nested relationships
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
[Introduction] Try to create a Ruby on Rails application
[Updated from time to time] Ruby on Rails Convenient methods
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
From Ruby on Rails error message display to Japanese localization
How to display a graph in Ruby on Rails (LazyHighChart)
I want to add a browsing function with ruby on rails
How to use Ruby on Rails
Steps to build a Ruby on Rails development environment with Vagrant
(Ruby on Rails6) Create a function to edit the posted content
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] Japanese notation of errors
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
[Ruby on Rails] Try to create a service that makes local cats happy
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
Introducing Rspec, a Ruby on Rails test framework
[Ruby on Rails] A memorandum of layout templates
[Rails] Introducing pay.jp (from view customization to registration)
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
I made a portfolio with Ruby On Rails
Settings to be done when changing from Sublime Text to VScode (when writing Ruby On Rails)
[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
Method summary to update multiple columns [Ruby on Rails]
Build a Ruby on Rails development environment on AWS Cloud9
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
Ruby Regular Expression Extracts from a specific string to a string
[Rails] How to load JavaScript in a specific view
(Ruby on Rails6) Reflecting the posted content from the form
(Ruby on Rails6) How to create models and tables
[Ruby on Rails] I get a warning when executing RSpec due to a different gem version.
[Ruby on Rails] Carousel of bootstrap4 is implemented as a slide show using each method.