ruby-on-rails - Rails 3 Controller 私有(private)方法作为辅助方法

标签 ruby-on-rails

是否有必要将 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/

相关文章:

css - 如何使带有 Rails 链接的 Div 可点击?

ruby-on-rails - 在 mongoid 关系上批量插入文档

ruby-on-rails - Heroku vs DotCloud vs Duostack vs 其他云/PaaS 提供商(Rails 和非 Rails)?

ruby-on-rails - 如何使用回形针保存raw_data照片

mysql - 具有相同时间戳的 Rails 迁移

ruby-on-rails - 从 Rails 启动系统进程

javascript - 使用 AWS JS SDK 时,请求 header 不包含 HTTP_X_CSRF_TOKEN

ruby-on-rails - 重写 Rails 辅助路由方法以通过 GET 参数设置布局

ruby-on-rails - 打破和返回值的最佳方式

ruby-on-rails - 在 sqlite3 数据库中保存 Rails marshal 时出现问题