rails中的更新路由对after_action反应不佳?

JaT*_*aTo 4 ruby routing ruby-on-rails ruby-on-rails-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
Run Code Online (Sandbox Code Playgroud)

大家好.我想知道是否有人可以向我解释为什么rails中的更新路由不能将我的after_action重定向(自定义方法在底部)带回家.我在after_action中包含更新时得到的错误是"缺少模板青蛙/更新".这将导致我在update方法中手动添加redirect_to frogs_path.

谢谢!

Rya*_*igg 8

after_action行动已经走完之后触发回调.您无法使用它来呈现或重定向.通过调用方法在动作本身内执行此操作:

def update
  ...
  redirect_home
end
Run Code Online (Sandbox Code Playgroud)