![](/img/trans.png)
[英]Are methods in controller after helper_method available to all views in rails
[英]Rails 3 controller private methods as helper method
是否有必要在控制器中提及控制器中的私有方法作为helper_methods
? 喜欢
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
必需? 如果是这样的话?
当我从控制器动作方法调用私有方法时,私有或helper
方法可以访问params
散列?
我认为你误解了'helper_method'的概念。
helper_method
用于使控制器方法充当辅助模块中的方法
因此,在您的控制器中,您始终可以访问您的私有方法,而无需“ helper_method
”部分
如果您将控制器方法添加为辅助方法,就像您已经完成的那样,在视图中您可以简单地调用它
对于你的第二个问题,yes params hash可以通过控制器私有方法访问
HTH
不,没有必要。 您始终可以在控制器中调用控制器的private
方法。
此外, params
将自动用于控制器内的private
方法。
无需在控制器中提及私有方法作为辅助方法。 您可以通过传递params或任何事物之类的参数,从同一控制器中的其他方法直接调用它们。
class ContorllerName < ApplicationController
def index
private_method(params)
end
private
def private_method(vals)
vals
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.