是否有必要将 Controller 中的私有(private)方法称为 helper_methods
在 Controller 中?
像
class PostsController < ApplicationController
helper_method :check_something
def new
check_something
@post = Post.new
end
def show
@post = Post.find(params[:id])
end
private
def check_something
redirect_to(root_path) and return if something
end
end
是声明:
helper_method :check_something
必需的 ?如果是,为什么?当我从 Controller 操作方法调用私有(private)方法时,
params
可在私有(private)或 helper
中访问的散列方法 ??
最佳答案
我认为您误解了“helper_method”的概念。helper_method
用于使您的 Controller 方法充当辅助模块中的方法
因此,在您的 Controller 内部,您始终可以访问您的私有(private)方法,而无需 ' helper_method
' 部分
如果您添加一个 Controller 方法作为辅助方法,就像您已经完成的那样,在您的 View 中您可以简单地调用它
对于您的第二个问题,是的,可以通过 Controller 私有(private)方法访问参数哈希
高温高压
关于ruby-on-rails - Rails 3 Controller 私有(private)方法作为辅助方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13854727/