控制器中的助手 - Rails 3

发布于 2024-12-27 18:11:51 字数 571 浏览 2 评论 0原文

我从 Rails 2.x 迁移到 3.x。现在当调用控制器方法时抛出

nil:NilClass 的未定义方法“my_helper_method”

MyController.rb

class MyController < ApplicationController
    def foo
      @template.my_helper_method
    end
end

MyControllerHelper.rb

class MyControllerHelper
    def my_helper_method
      puts "Hello"
    end
end

ApplicationController

class ApplicationController < ActionController::Base
   helper :all
end

如何让它工作?

I migrated from rails 2.x to 3.x. Now when calling a controller method throws

undefined method `my_helper_method' for nil:NilClass

MyController.rb

class MyController < ApplicationController
    def foo
      @template.my_helper_method
    end
end

MyControllerHelper.rb

class MyControllerHelper
    def my_helper_method
      puts "Hello"
    end
end

ApplicationController

class ApplicationController < ActionController::Base
   helper :all
end

How to get this working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放赐 2025-01-03 18:11:51

这实际上在另一篇SO帖子中得到了回答:Rails 3:@template变量内部控制器为零

本质上,您可以将@template替换为view_context

This is actually answered in another SO post: Rails 3: @template variable inside controllers is nil

Essentially, you can replace @template with view_context

诺曦 2025-01-03 18:11:51

@template 是一个对象,在您的情况下是nil。如果此对象中没有方法 (my_helper_method),则无法调用它(尤其是当它为 nil 时)。

在帮助器中定义的方法像常规方法一样被调用。但不是在控制器中,它们是在视图中调用的。您的 helper :all 只是使所有助手都可用于视图。

因此,在您看来: my_helper_method :arg1, :arg2

如果您的对象需要一个方法 (@template),则需要为您的对象提供此方法。

示例:

class Template < ActiveRecord::Base

  def my_helper_method
    # do something on a template instance
  end

end


class MyController < ApplicationController
  def foo
    @template = Template.first
    @template.my_helper_method # which actually isn't a helper
  end
end

助手的作用:

module MyHelper
  def helper_method_for_template(what)
  end
end

# in your view
helper_method_for_template(@template)

混合助手(将视图助手与视图和模型混合时,请注意代码中是否会出现混乱)

class Template < ActiveRecord::Base
  include MyHelper

  # Now, there is @template.helper_method_for_template(what) in here. 
  # This can get messy when you are making your helpers available to your
  # views AND use them here. So why not just write the code in here where it belongs
  # and leave helpers to the views? 
end

@template is an object, in your case nil. If this object doesn't has the method (my_helper_method) in it, you cannot call it (especially not if it is nil).

Methods defined in helpers are called like regular methods. But not in controllers, they are called in views. Your helper :all just makes all helpers available to the views.

So, in your view: my_helper_method :arg1, :arg2

IF you need a method for your object (@template), you need to give your object this method.

Example:

class Template < ActiveRecord::Base

  def my_helper_method
    # do something on a template instance
  end

end


class MyController < ApplicationController
  def foo
    @template = Template.first
    @template.my_helper_method # which actually isn't a helper
  end
end

What helpers do:

module MyHelper
  def helper_method_for_template(what)
  end
end

# in your view
helper_method_for_template(@template)

Mixing in a helper (be aware of having a mess in your code when mixing view helpers with views and models)

class Template < ActiveRecord::Base
  include MyHelper

  # Now, there is @template.helper_method_for_template(what) in here. 
  # This can get messy when you are making your helpers available to your
  # views AND use them here. So why not just write the code in here where it belongs
  # and leave helpers to the views? 
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文