rails:如何访问应用程序控制器中的方法?

[英]rails: how do i access a method in my application controller?


Noob scoping issue, I imagine. :\

我想,Noob范围问题。 :\

class ApplicationController < ActionController::Base
  protect_from_forgery

  @locations = get_locations

  def get_locations
    Location.where(:active => true).order('name').all
  end

end

Error:

undefined local variable or method `get_locations' for ApplicationController:Class

Two questions: 1) What's with the error? Am I calling the method incorrectly? 2) How do I access this method from a sub-classed controller?

两个问题:1)错误是什么?我是否错误地调用了该方法? 2)如何从子类控制器访问此方法?

4 个解决方案

#1


9  

You're calling get_locations within the class scope, but the method is an instance method, not a class method. If for example you used def self.get_locations then you would be providing a class method, one of which you can use within the class scope (after you have defined it, not before like you're doing).

您在类范围内调用get_locations,但该方法是实例方法,而不是类方法。例如,如果您使用def self.get_locations,那么您将提供一个类方法,其中一个可以在类范围内使用(在您定义之后,而不是像您之前那样)。

The problem here is the logic, what is this method for? What do you intend to use @locations for? If it's to go inside your application view, then you should put this method into the ApplicationHelper module, and call it from inside the relevant action. If you'd like it in another view on another controller and you'd like to use @locations inside your locations method, perhaps your setup might look something like this:

这里的问题是逻辑,这个方法是什么?您打算如何使用@locations?如果要进入应用程序视图,那么应该将此方法放入ApplicationHelper模块,并从相关操作内部调用它。如果您想在另一个控制器上的另一个视图中使用它并且您想在您的位置方法中使用@locations,那么您的设置可能如下所示:

PagesController

class PagesController < ActionController::Base
  def locations
    @locations = Location.where(:active => true).order('name').all
  end
end

locations.html.erb

<% @locations.each do |location| %>
  <%= # do something with 'location' %>
<% end %>

If you'd like to use this inside of your application.html.erb you can simplify it quite some..

如果你想在application.html.erb中使用它,你可以简化一些..

ApplicationController

class ApplicationController < ActionController::Base
  protect_from_forgery

  def locations
    Location.where(:active => true).order('name').all
  end
 end

application.html.erb

<% locations.each do |location| %>
  <%= # do something with location %>
<% end %>

The answer boils down to logic, and to really figure out exactly what you're looking for, more details would probably be required.

答案归结为逻辑,并且要真正弄清楚您正在寻找什么,可能需要更多细节。

#2


3  

You're calling it from the class scope, not from an instance scope. more likely what you want is the following:

您是从类范围调用它,而不是从实例范围调用它。您想要的更多可能是以下内容:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :setup_locations


  private
  def setup_locations
    @locations = Location.where(:active => true).order('name').all
  end

end

To make your original example work, you'd need to make #get_locations defined on self (which points to the class at definition), like so:

要使原始示例有效,您需要在self上定义#get_locations(在定义时指向类),如下所示:

class ApplicationController < ActionController::Base
  protect_from_forgery

  @locations = get_locations

  def self.get_locations
    Location.where(:active => true).order('name').all
  end

end

The problem with that code is that @locations will only be available from the class level as a class instance variable, which is comparable to a static variable in most other languages, and which probably isn't what you want.

该代码的问题在于@locations只能从类级别作为类实例变量使用,它与大多数其他语言中的静态变量相当,而且可能不是您想要的。

#3


1  

I imagine that this line:

我想这一行:

@locations = get_locations

... is trying to access the class level method get_locations and not the instance method.

...正在尝试访问类级方法get_locations而不是实例方法。

The clue here is that the error message is showing that it can't find it on the class itself (ApplicationController:Class) and not an instance of that class. That means that you're in the class scope, not instance scope.

这里的线索是错误消息显示它无法在类本身(ApplicationController:Class)上找到它而不是该类的实例。这意味着您在类范围内,而不是实例范围。

This would fix it:

这会解决它:

  def self.get_locations
     Location.where(:active => true).order('name').all
  end

#4


1  

Even the question is quite old, you can also call your controller action anywhere just by calling:

即使问题很老,您也可以通过调用以下方式调用控制器操作:

ApplicationController.new.get_locations

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/12/07/456606d73d10c5cb36acd7e610acf731.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号