简体   繁体   English

ruby on rails在助手与控制器中的应用范围广泛的方法?

[英]ruby on rails application wide method in helper vs. controller?

I have several variables that I would like all controllers to access. 我有几个变量,我希望所有控制器访问。 So I defined them in my application_controller.rb : 所以我在application_controller.rb中定义了它们:

      before_filter :initialize_vars

  def initialize_vars
    @siteTitle = "my title"
    @companyName = "company"    
  end

No problems there. 没有问题。 I wanted to do something similar with the logo so I created another method that was called with the before_filter as well. 我想用徽标做类似的事情,所以我创建了另一个用before_filter调用的方法。

  def logo
    image_tag("Logo.jpg", :alt => "Logo")
  end

one instance of the logo img should link to the site root so I called it with: 徽标img的一个实例应链接到站点根目录,因此我调用它:

<%=h link_to logo, root_path %>

but it didn't work in my layout! 但它在我的布局中不起作用! When I add my logo method to the application_helper.rb everything works perfectly. 当我将我的logo方法添加到application_helper.rb时,一切都很完美。 hhmmm. hhmmm。

what / where is the appropriate place for all this stuff? 什么/适合这些东西的适当位置? I mean just because I was able to make it work doesn't make it right! 我的意思是因为我能够使它工作不正确!

Should I define my instance variables (that I'm treating like global variables) in the application_controller and the logo method in my helper like I've done? 我应该在application_controller中定义我的实例变量(我将其视为全局变量)和我帮助器中的logo方法,就像我已经完成的那样? I feel like I'm missing some fundamental understanding here of why they need to go in different places. 我觉得我在这里缺少一些基本的理解,为什么他们需要去不同的地方。 I'm not sure if it's HOW I'm calling the "logo" method or where I'm putting it. 我不确定我是否正在调用“徽标”方法或者我正在使用它。 I'm going to play with how I'm calling and how I've written the logo method because I feel like both methods should go in the application_controller. 我将使用我如何调用以及如何编写徽标方法,因为我觉得这两种方法都应该放在application_controller中。

thoughts? 想法?

Thanks! 谢谢!

Functions that are related to rendering views are placed in helper files. 与渲染视图相关的函数放在辅助文件中。 They typically generate HTML content. 它们通常生成HTML内容。 If the helper method is used across the app in many places, put them in application_helper.rb , otherwise they have to be placed in corresponding helper files. 如果在许多地方在应用程序中使用辅助方法,请将它们放在application_helper.rb ,否则必须将它们放在相应的帮助文件中。

As the instance variables you have is going to be accessed in many controllers, you can initialize them in application controller like you have. 由于您将在许多控制器中访问实例变量,因此可以像应用程序控制器一样在初始化它们。

Helper methods are used for methods pertaining to the views as it is your views that use the helper methods. Helper方法用于与视图相关的方法,因为它是使用帮助器方法的视图。 The instance variables you have look like they should be refactored into methods that utilize content_for and then yield them in your main layout file. 您看起来的实例变量应该重构为利用content_for然后在主布局文件中生成它们的方法。

http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

http://railscasts.com/episodes/8-layouts-and-content-for http://railscasts.com/episodes/8-layouts-and-content-for

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

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