4
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!

4

1 回答 1

8

after_action操作完成后触发回调。您不能使用它来呈现或重定向。通过调用方法在操作本身内执行此操作:

def update
  ...
  redirect_home
end
于 2013-11-06T01:18:35.240 回答