rails 中的更新路线对 after_action 反应不佳? [英] Update route in rails doesn't respond well to after_action?

查看:60
本文介绍了rails 中的更新路线对 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

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

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.

谢谢!

推荐答案

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

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

这篇关于rails 中的更新路线对 after_action 反应不佳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆