使用“helper_method”时遇到问题和渲染模板
我正在使用 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://
但如果我以这种方式将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
helper_method 实际上与accounts_helper.rb 中的定义方法几乎相同(从技术上讲,它针对助手类评估代码)。为了使用这个帮助器,您需要在帮助器模块中定义它,并将其包含在要渲染的显示模板的控制器中。
实际的问题是,不同的控制器对您的帐户控制器助手一无所知,除非您明确要求包含它们。
示例:
所有帐户帮助程序方法将在应用程序中可用
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:
all account helper methods will be available across application