Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update route in rails doesn't respond well to after_action?

class FrogsController < ApplicationController
  before_action :find_frog, only: [:edit, :update, :show, :destroy]
  after_action :redirect_home, only: [:update, :create, :destroy]

  def index
    @frogs = Frog.all
  end

  def new
    @ponds = Pond.all
    @frog = Frog.new
  end

  def create
    @frog = Frog.create(frog_params)
  end

  def edit
    @ponds = Pond.all
  end

  def update
    @frog.update_attributes(frog_params)

  end

  def show
  end

  def destroy
    @frog.destroy
  end

  private
  def find_frog
    @frog = Frog.find(params[:id])
  end

  def frog_params
    params.require(:frog).permit(:name, :color, :pond_id)
  end

  def redirect_home
    redirect_to frogs_path
  end
end

Hi all. I was wondering if someone could explain to me why the update route in rails can't take my after_action of redirecting (custom made method on the bottom) it home. The error that I get when i include update in the after_action is "Missing template frogs/update". This is going to cause me to manually add a redirect_to frogs_path inside the update method.

thanks!

like image 986
JaTo Avatar asked Feb 15 '23 18:02

JaTo


1 Answers

The after_action callback is triggered after the action has run its course. You cannot use it to render or redirect. Do that within the action itself by calling the method:

def update
  ...
  redirect_home
end
like image 166
Ryan Bigg Avatar answered Feb 17 '23 19:02

Ryan Bigg