Ruby / Rails - 如何创建类并从控制器访问它 [英] Ruby/Rails - How to Create a Class and Access it from the Controller

查看:87
本文介绍了Ruby / Rails - 如何创建类并从控制器访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用一些不同的gems在rails 3中显示googlemaps,并有几个问题。



幸运的是,我发现这https://github.com/YouthTree/bhm-google-maps 这是一个帮手,它似乎为其他人工作。



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



他们给的例子是

  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

然后运行

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




很简单,但我没有经历需要在rails中创建一个类,所以我有一些问题。



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



我可以在我的控制器中创建这个类吗?



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

解决方案

应该把 location.rb 放在你觉得最合理的地方。让它在 app / models / location.rb 将确保它是在应用程序启动时自动需要的,但有些人希望在 app / models 由ActiveRecord支持。



您也可以将它放在 lib /



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

  require#{RAILS.root} /lib/location.rb 

至于在你的控制器中创建它肯定是一个类的另一个实例:

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

然后在您的视图中:

 <%= draw_map_of @location%> 

不要忘记 - 下面的Rails是纯Ruby的所有力量和灵活性,准备好使用你不仅限于Rails给了你什么。


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

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

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

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.

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????)

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.

解决方案

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

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

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

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.

这篇关于Ruby / Rails - 如何创建类并从控制器访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆