Ruby/Rails - 如何创建一个类并从控制器访问它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5174377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:34:09  来源:igfitidea点击:

Ruby/Rails - How to Create a Class and Access it from the Controller

ruby-on-railsrubyclass

提问by ChrisWesAllen

I've been trying to use a few different gems for displaying googlemaps within rails 3 and have had several problems.

我一直在尝试使用一些不同的 gem 在 rails 3 中显示谷歌地图,但遇到了几个问题。

Luckily I found this https://github.com/YouthTree/bhm-google-mapswhich is a helper and it seems to work for others.

幸运的是,我找到了这个https://github.com/YouthTree/bhm-google-maps,它是一个帮手,似乎对其他人有用。

I've installed it properly but in the readme https://github.com/YouthTree/bhm-google-maps/blob/master/README.mdit mentions creating a class for the object to display in the view.

我已经正确安装了它,但是在自述文件https://github.com/YouthTree/bhm-google-maps/blob/master/README.md 中它提到为要在视图中显示的对象创建一个类。

The example they gave was

他们举的例子是

class Location
   attr_accessor :address, :lat, :lng
   def initialize(address, lat, lng)
      @address = address
      @lat = lat
      @lng = lng
   end
  def to_s; address.to_s; end
end

And then running

然后运行

 <%= draw_map_of Location.new("My House", 12.345, 56.789) %>

in the view.

在视图中。

It seems simple enough but I haven't experienced the need of creating a class before in rails so I have some questions.

看起来很简单,但我之前没有经历过在 rails 中创建类的需要,所以我有一些问题。

Should I create a location.rb file and place the above code in it, but where should I place the file? (model folder, application folder????)

我应该创建一个 location.rb 文件并将上面的代码放入其中,但是我应该将文件放在哪里?(模型文件夹,应用程序文件夹????)

Is there a way for me to create this class within my controller?

有没有办法在我的控制器中创建这个类?

Ideally I would like to manipulate the lat/lng values as variables and display a dynamic map.

理想情况下,我想将 lat/lng 值作为变量进行操作并显示动态地图。

回答by Dan Cheail

You should put location.rbwherever you feel it makes the most sense. Having it at app/models/location.rbwill ensure that it's automatically required when your app starts, but some people expect that classes in app/modelsare backed by ActiveRecord.

你应该把location.rb它放在你觉得最有意义的地方。随意使用它app/models/location.rb可以确保在您的应用程序启动时自动需要它,但有些人希望其中的类app/models由 ActiveRecord 支持。

You could also put it under lib/if you prefer.

lib/如果你愿意,你也可以把它放在下面。

To make it available to the app, you can include require statement in project initializers inside your config folder:

要使其可用于应用程序,您可以在 config 文件夹内的项目初始值设定项中包含 require 语句:

require "#{Rails.root}/lib/location.rb"

As for creating it inside your Controller - definitely! It's just another instance of a class:

至于在您的控制器中创建它 - 当然!它只是一个类的另一个实例:

def show
  @location = Location.new("My House", 12.345, 56.789)
end

And then in your view:

然后在你看来:

<%= draw_map_of @location %>

Don't forget – beneath Rails is all the power and flexibility of pure Ruby, ready to be used. You're not only limited to what Rails gives you.

不要忘记 - 在 Rails 之下是纯 Ruby 的所有功能和灵活性,随时可以使用。您不仅限于 Rails 为您提供的功能。