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.
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↓
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.
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.
Let's look at each one.
Check the routing again.
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.
If you check the contents of the params that have been brought to create and update,
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)
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.
https://stackoverflow.com/questions/20368717/rails-i-cant-pass-a-validation-error-in-a-redirect
Recommended Posts