使用“helper_method”时遇到问题和渲染模板

发布于 2024-10-26 06:37:36 字数 1492 浏览 4 评论 0原文

我正在使用 Ruby on Rails 3,我正在尝试设置一个 helper_method ,它应该仅适用于控制器(例如:AccountsController)以及与之相关的所有视图,以及当其视图在另一个控制器中呈现时与该控制器无关的视图。我从Railcast“限制访问”中获得灵感。

在我的accounts_controller.rb 文件中,

# Just to know, I am using a User namespace...
class Users::AccountsController < ApplicationController
  helper_method :show_authorization

  def show_authorization
    false # This returning value is just an example
  end
end

在我的views/users/accounts/show.html.erb 文件中,

<% if show_authorization %>
  You are authorized!
<% else %>
  You are NOT authorized!
<% end %>

如果我浏览URL http:///users/accounts/1,则上述代码有效 但如果我以这种方式将 show.html.erb 文件渲染为另一个视图文件中的模板:

<%= render :template => "/users/accounts/show", :locals => { :account => @account } %>

我收到错误:

NameError in Users#show 
undefined local variable or method `show_authorization' for #<#<Class:...>

为什么?我该如何解决这个问题,以便在与另一个控制器相关的另一个视图中呈现时,使 AccountsController show_authorization 方法可用于 show.html.erb 视图? strong>

PS:由于 show_authorization 仅与 AccountsController 及其视图文件相关,因此我不想在“application_controller.rb”中插入相关代码文件,但将其保留在“accounts_controller.rb”中。

I am using Ruby on Rails 3 and I am trying to set an helper_method that should work just for a controller (example: AccountsController) and for all views related to that, also when its views are rendered in another views not related to that controller. I take inspiration from the Railcast "Restricting Access".

In my accounts_controller.rb file I have

# Just to know, I am using a User namespace...
class Users::AccountsController < ApplicationController
  helper_method :show_authorization

  def show_authorization
    false # This returning value is just an example
  end
end

In my views/users/accounts/show.html.erb file I have

<% if show_authorization %>
  You are authorized!
<% else %>
  You are NOT authorized!
<% end %>

The above code works if I browse the URL http://<my_app_name>/users/accounts/1 but if I render the show.html.erb file as a template in another view file this way:

<%= render :template => "/users/accounts/show", :locals => { :account => @account } %>

I get the error:

NameError in Users#show 
undefined local variable or method `show_authorization' for #<#<Class:...>

Why? How can I solve that in order to make the AccountsController show_authorization method available to the show.html.erb view when that is rendered in another view related to another controller?

P.S.: Since the show_authorization is related only to the AccountsController and its views file, I would like to don't insert the related code in the 'application_controller.rb' file but keep that in the 'accounts_controller.rb'.

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

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

发布评论

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

评论(1

习惯成性 2024-11-02 06:37:36

helper_method 实际上与accounts_helper.rb 中的定义方法几乎相同(从技术上讲,它针对助手类评估代码)。为了使用这个帮助器,您需要在帮助器模块中定义它,并将其包含在要渲染的显示模板的控制器中。

实际的问题是,不同的控制器对您的帐户控制器助手一无所知,除非您明确要求包含它们。

示例:

module Users::AccountsHelper
  code_here
end

class ApplicationHelper
  helper Users::AccountsHelper
end

所有帐户帮助程序方法将在应用程序中可用

The helper_method is practically almost the same as a defining method in accounts_helper.rb (technically it eval code against helper class). In order to use this helper you need to define it in a helper module and include it in controllers where show template meant to be rendered.

The actual problem is that different controller would know nothing about your accounts controller helpers until you explicitly require to include them.

Examples:

module Users::AccountsHelper
  code_here
end

class ApplicationHelper
  helper Users::AccountsHelper
end

all account helper methods will be available across application

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