ruby on Rails 在帮助程序与控制器中应用广泛的方法?
我希望所有控制器都能访问几个变量。所以我在 application_controller.rb 中定义了它们:
before_filter :initialize_vars
def initialize_vars
@siteTitle = "my title"
@companyName = "company"
end
没有问题。 我想对徽标做类似的事情,所以我创建了另一个方法,也使用 before_filter 调用。
def logo
image_tag("Logo.jpg", :alt => "Logo")
end
徽标 img 的一个实例应该链接到站点根目录,因此我用以下方式调用它:
<%=h link_to logo, root_path %>
但它在我的布局中不起作用!当我将徽标方法添加到 application_helper.rb 时,一切正常。嗯嗯。
所有这些东西的合适位置是什么/哪里?我的意思是,仅仅因为我能够让它发挥作用,并不意味着它就是正确的!
我是否应该像我所做的那样在 application_controller 中定义实例变量(我将其视为全局变量)并在助手中定义徽标方法?我觉得我在这里缺少一些关于为什么他们需要去不同地方的基本理解。我不确定我是如何调用“logo”方法的还是我把它放在哪里。我将尝试如何调用以及如何编写徽标方法,因为我觉得这两种方法都应该放入 application_controller 中。
想法?
谢谢!
I have several variables that I would like all controllers to access. So I defined them in my 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.
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:
<%=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. 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? 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.
thoughts?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与渲染视图相关的函数放置在辅助文件中。
它们通常生成 HTML 内容。如果应用程序中很多地方都使用了辅助方法,请将它们放在
application_helper.rb
中,否则必须将它们放在相应的辅助文件中。由于您拥有的实例变量将在许多控制器中访问,因此您可以像您一样在应用程序控制器中初始化它们。
Functions that are related to rendering views are placed in helper files.
They typically generate HTML content. 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.As the instance variables you have is going to be accessed in many controllers, you can initialize them in application controller like you have.
辅助方法用于与视图相关的方法,因为您的视图使用辅助方法。您拥有的实例变量看起来应该被重构为利用 content_for 的方法,然后在主布局文件中生成它们。
http://api.rubyonrails.org/classes/ActionView /Helpers/CaptureHelper.html#method-i-content_for
http://railscasts.com/episodes/8-layouts-and-content-for
Helper methods are used for methods pertaining to the views as it is your views that use the helper methods. 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.
http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for
http://railscasts.com/episodes/8-layouts-and-content-for