3

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

在我的 Post 控制器中,我没有方法 show (未描述),但我在控制器中放入:

def method_missing(name, *args)
  redirect_to posts_path
end

我认为如果控制器找不到动作显示 - 它会调用method_missing并在重定向到index方法之后,但 Rails 会尝试渲染 view show.html.erb

为什么方法缺失不捕获?我该如何使用method_missing

4

1 回答 1

4

Rails does not require action to be present in controller if corresponding template exists. It just assumes empty action and renders template, that is why your method_missing isn't invoked.

If you don't need show action anyway - just remove show.html.erb and method_missing will work as expected.

于 2012-11-03T07:49:06.820 回答