使用'helper_method'和渲染模板的麻烦 [英] Trouble using 'helper_method' and rendering templates

查看:151
本文介绍了使用'helper_method'和渲染模板的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在我的accounts_controller.rb文件中我有

 #只是知道,我使用User命名空间... 
class Users :: AccountsController< ApplicationController
helper_method:show_authorization

def show_authorization
false#这个返回值只是一个例子
end
end



在我的意见/ users / accounts / show.html.erb文件中,我有

 <%if show_authorization%> 
您已获授权!
<%else%>
您没有授权!
<%end%>

如果我浏览URL http://< my_app_name> / users / accounts / 1 但是如果我将 show.html.erb 文件作为另一个视图文件中的模板, / p>

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

我得到错误:

 用户中的NameError#show 
未定义的局部变量或方法`show_authorization'for#<#< Class:...&

为什么?我如何解决这个问题,以便在 show.html.erb 视图中提供AccountsController show_authorization

show_authorization 只与AccountsController及其视图文件相关,我想不要插入相关的代码在'application_controller.rb'文件,但保留在'accounts_controller.rb'。

$ helper_method几乎和accounts_helper.rb中的定义方法几乎相同(从技术上讲,它是针对辅助类的eval代码)。为了使用这个帮助器,你需要在一个帮助模块中定义它,并将其包含在控制器中,其中的show template意在被渲染。



实际的问题是,不同的控制器



示例:

  module Users :: AccountsHelper 
code_here
end

class ApplicationHelper
helper Users :: AccountsHelper
end

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


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'.

解决方案

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

这篇关于使用'helper_method'和渲染模板的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆