It is a memorandum. Thank you for browsing. Since it is a scribble, please point out any mistakes in the explanation or description.
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
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"}
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.
To be able to understand the processing flow as to why and why the action :: show could not be redirected.
Recommended Posts