helper 和 helper_method 的作用是什么?

发布于 2024-09-28 09:50:42 字数 363 浏览 3 评论 0原文

helper_method 很简单:它使控制器的部分或全部方法可供视图使用。

什么是助手?是否相反,即将辅助方法导入到文件或模块中? (也许名称 helperhelper_method 很相似。它们可能是 share_methods_with_viewimport_methods_from_view

参考

helper_method is straightforward: it makes some or all of the controller's methods available to the view.

What is helper? Is it the other way around, i.e., it imports helper methods into a file or a module? (Maybe the name helper and helper_method are alike. They may rather instead be share_methods_with_view and import_methods_from_view)

reference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-10-05 09:50:42

helper_method 方法是显式共享控制器中定义的一些方法,使它们可供视图使用。这用于您需要从控制器和帮助器/视图访问的任何方法(标准帮助器方法在控制器中不可用)。例如,常见用例:

#application_controller.rb
def current_user
  @current_user ||= User.find_by_id!(session[:user_id])
end
helper_method :current_user

另一方面,helper 方法用于将整个助手导入到控制器(及其继承的控制器)提供的视图中。这意味着

# application_controller.rb
helper :all

For Rails > 3.1

# application.rb
config.action_controller.include_all_helpers = true
# This is the default anyway, but worth knowing how to turn it off

使所有辅助模块可用于所有视图(至少对于从 application_controller 继承的所有控制器)。

# home_controller.rb
helper UserHelper

使 UserHelper 方法可用于视图以执行主控制器的操作。这相当于执行以下操作:

# HomeHelper
include UserHelper

The method helper_method is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and helpers/views (standard helper methods are not available in controllers). e.g. common use case:

#application_controller.rb
def current_user
  @current_user ||= User.find_by_id!(session[:user_id])
end
helper_method :current_user

the helper method on the other hand, is for importing an entire helper to the views provided by the controller (and it's inherited controllers). What this means is doing

# application_controller.rb
helper :all

For Rails > 3.1

# application.rb
config.action_controller.include_all_helpers = true
# This is the default anyway, but worth knowing how to turn it off

makes all helper modules available to all views (at least for all controllers inheriting from application_controller.

# home_controller.rb
helper UserHelper

makes the UserHelper methods available to views for actions of the home controller. This is equivalent to doing:

# HomeHelper
include UserHelper
初心 2024-10-05 09:50:42

Helper 方法用于执行跨多个类常见的特定重复任务。这可以防止我们在不同的类中一次又一次地重复相同的代码。

下面是一个简化上述定义的示例:

这是一段代码,您将在视图中看到类似这样的内容:

<% if @user && @user.email.present? %>
  <%= @user.email %>
<% end %>

我们可以稍微清理一下并将其放入助手中:

module SiteHelper
  def user_email(user)
    user.email if user && user.email.present?
  end
end

然后在视图代码中,您调用helper 方法并将其作为参数传递给用户。

<%= user_email(@user) %>

这种提取使视图代码更易于阅读,特别是如果您明智地选择辅助方法名称。

所以我希望这能让你明白一些事情。

报价来源
代码源

A Helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again and again.

Here's an example to simplify the above definition:

Here is a code, where you would have something like this in the view:

<% if @user && @user.email.present? %>
  <%= @user.email %>
<% end %>

We can clean it up a little bit and put it into a helper:

module SiteHelper
  def user_email(user)
    user.email if user && user.email.present?
  end
end

And then in the view code, you call the helper method and pass it to the user as an argument.

<%= user_email(@user) %>

This extraction makes the view code easier to read especially if you choose your helper method names wisely.

So I hope this clears things up a little for you.

Source for the quotation
Source for the code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文