我在app/helpers/posts_helpers.rb中有一个自定义帮助器
module PostsHelper
def custom_helper(post)
#do something
end
end
当我从一个视图中调用它时,我得到了错误:
ActionView::Template::Error (undefined method 'custom_helper' for class..
但是,如果我像下面这样从app/helpers/application_helper.rb调用相同的helper,那么视图就能够检测到helper。
module A
我正在使用ACL将资源授予系统中的角色,允许的操作被执行,拒绝的操作被路由到自定义页面,我希望在运行时使用ACL的资源显示和隐藏菜单元素,还希望在视图中显示和隐藏锚点和按钮。
我创建了一个helper类
class Zend_View_Helper_Permission extends Zend_View_Helper_Abstract
{
private $_acl;
public function hasAccess($role, $action, $controller)
{
if (!$this->_acl) {
此视图:
@Using Html.BeginForm
SimpleMessage
End Using
@Helper SimpleMessage()
@<p>This is a simple message</p>
End Helper
结果:
<form action="/Controller/Action" method="post"></form>
但
@* Now let's not call it from within a using *@
@SimpleMessage
@H
在普通的视图规范文件中,我可以使用Capybara测试视图。
require 'spec_helper'
describe 'site/login' do
subject { page }
before { visit login_path }
it { should have_selector('#login form') }
end
使用Rspec,我可以测试parital。
require 'spec_helper'
describe 'site/sidebar' do
it "
问题询问如何将Rails视图帮助器函数提取到gem中,接受的答案非常好。
我想知道--如何为Sinatra做同样的事情?我正在制作一个gem,它在一个模块中定义了一组helper函数,我想让这些函数对Sinatra视图可用。但是无论我怎么尝试,我似乎都无法访问这些函数,我只是得到了一个undefined local variable or method错误。
到目前为止,我的gem结构看起来像这样(省略了其他东西,比如gemspec ):
cool_gem/
lib/
cool_gem/
helper_functions.rb
sinatra.rb
c
我想提供一个符合UITableViewDataSource协议的辅助视图控制器。但是这个类不是有意的,必须通过继承来使用。
为此,编译器要求我在helper类中实现协议。我怎么才能解决这个问题?
class BigHeadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Helper class that do ton of the job
}
class StoryTableViewController: BigHeadViewController {
//
我想知道是否有可能在View Helper中使用mulitple函数?假设我有这个视图帮助器:
class Zend_View_Helper_Time extends Zend_View_Helper_Abstract {
public function time($date) {
// Some code here
}
}
在我看来,我将这样使用它:
$this->time($some_date);
但是,如果我愿意,在同一个helper中能够调用另一个方法,比如:
$this->Time->convertMyDate($some_date
我有一个视图助手类
namespace Users\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Userfunctions extends AbstractHelper
{
protected $count = 0;
public function test()
{
return 'testing';
}
public function __invoke($str, $find)
{
if (! is_string($str
我正在将继承的ZendFramework2应用程序迁移到ZendFramework3,注册我的自定义表单视图帮助程序遇到了一些困难。当应用程序使用版本2时,帮助程序工作,并且主要用于添加标记属性以实现可访问性。例如,这是一个定制的FormText.php助手。
<?php
namespace Application\Form\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormInput;
class FormText extends FormInput
{
/**
我有两个控制器,它们有一个帮助器方法,具有相同的名称,但返回不同的内容: class My1Controller < ApplicationController
helper_method :my_helper
def show
...
end
private
def my_helper
"Something from My1 controller"
end
end
class My2Controller < ApplicationController
helper_method :my_helper
d
class My_View_Helper_Gender extends Zend_View_Helper_Abstract
{
public function Gender()
{
//
}
}
"The class method (Gender()) must be named identically to the concliding part
of your class name(Gender).Likewise,the helper's file name must be named
identically to the method,an