我正在编写一个Rails插件,可以在视图中构建菜单.我正在使用link_to
构建链接并在当前页面上current_page?
设置class="active"
.
我include
ð ActionView::Helpers::UrlHelper
这样我就可以使用link_to
.
为了current_page?
在视图中工作,我必须继承当前的类(显然ActionView::Base
)并且它非常有效.不幸的是,这完全打破了RSpec的测试.
我打电话link_to
和current_page?
这样的:
def menu(options = {}, &block)
carte = my_menu.new(self)
yield carte
carte.to_s unless carte.empty?
end
class my_menu
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
def initialize(base)
@base = base
end
def link
@base.link_to(name, url_options, html_options)
@base.current_page?(url_options)
end
end
Run Code Online (Sandbox Code Playgroud)
我用RSpec得到了这个错误:未定义的方法`link_to'用于#<Spec :: Rails :: Example :: RailsExampleGroup :: Subclass_1:0x24afa90>
任何线索?
我想在编写器访问器中进行一些检查.我的第一个想法是返回一个布尔值.
class MyClass
def var=(var)
@var = var
# some checking
return true
end
end
m = MyClass.new
retval = (m.var = 'foo')
=> "foo"
Run Code Online (Sandbox Code Playgroud)
我可以在编写器访问器中设置返回值吗?如果是,我怎样才能得到这个值?