加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在rails中更新路由对after_action没有很好的响

发布时间:2020-12-17 03:40:42 所属栏目:百科 来源:网络整理
导读: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.n
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中包含更新时得到的错误是“缺少模板青蛙/更新”.
这将导致我在update方法中手动添加redirect_to frogs_path.

谢谢!

解决方法

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

def update
  ...
  redirect_home
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读