gpt4 book ai didi

ruby-on-rails - 理论 : method_missing

转载 作者:行者123 更新时间:2023-12-04 19:50:40 25 4
gpt4 key购买 nike

我刚开始学习 Rails,我无法理解:

在我的 Post Controller 中,我没有显示方法(未描述),但我在我的 Controller 中放入了:

def method_missing(name, *args)
redirect_to posts_path
end

我认为如果 Controller 找不到 Action 显示 - 它会调用 method_missing 然后重定向到 index 方法,但 Rails 会尝试渲染 View 显示.html.erb

为什么 method missing 没有 catching?如何使用 method_missing

最佳答案

如果存在相应的模板,Rails 不需要在 Controller 中出现操作。它只是假定为空操作并呈现模板,这就是为什么您的 method_missing 未被调用的原因。

如果您不需要 show 操作 - 只需删除 show.html.erb 并且 method_missing 将按预期工作。

关于ruby-on-rails - 理论 : method_missing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13207058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com