简体   繁体   English

Ruby on Rails控制器视图重构

[英]Ruby on Rails controller-view refactoring

In my app I am using the ym4r-gm plugin, which allows you to play with the Google Maps API... I put the map "setup" in the controller: 在我的应用程序中,我使用的是ym4r-gm插件,该插件可让您使用Google Maps API ...我将地图“设置”放入控制器中:

@map = GMap.new("div_map")
@map.control_init(:large_map => true, :map_type => true)
@map.center_zoom_init([47.0, 26.0], 7)
...

And only render @map in the view. 并且仅在视图中呈现@map。

So my first question is whether I am using the right approach of "diving" this code? 因此,我的第一个问题是我是否使用了“潜水”此代码的正确方法? And the second question is: I have to models, which are rendering the same map (only the resources are different). 第二个问题是:我必须建模,它们渲染相同的地图(只有资源不同)。 Where should I put my refactored method that renders the map? 我应该在哪里放置渲染地图的重构方法? In the application controller, maybe? 在应用程序控制器中,也许吗?

Thanks in advance, I hope you will understand me! 在此先感谢,希望您能理解我!

Depending on the size of you project. 取决于项目的大小。 You could add it to the application controller, in case you want other controllers to take advantage of it. 您可以将其添加到应用程序控制器中,以防其他控制器利用它。 Or you could create a new module and put your code in there, this way you will need to include it into models/controllers that need it only. 或者,您可以创建一个新模块并将代码放入其中,这样您将需要将其包含在仅需要它的模型/控制器中。

I'd prefer the latter, since it is more structured that way and application controller is not cluttered. 我更喜欢后者,因为它以这种方式更加结构化并且应用程序控制器不会混乱。 But this is really a personal choice. 但这确实是个人选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM