disclaimer I am pretty new to Ruby / Rails
免责声明我是Ruby / Rails的新手
I am using RPH::Navigation for creating my navigation menus. What I am trying to do now is add some sub-nav items and I am doing so like this:
我正在使用RPH :: Navigation来创建我的导航菜单。我现在要做的是添加一些子导航项目,我这样做:
suboptions.each do |group|
def relpath
link(group[:link], group[:args]).
end
sub_menu.item group[:name], :path => :relpath
end
link
is just a method that returns "#{link}?#{query_string}"
where query_string
is built from the args
hash.
link只是一个返回“#{link}?#{query_string}”的方法,其中query_string是从args哈希构建的。
What I was originally trying to do was something like
我原本想做的事情是这样的
sub_menu.item group[:name], :path => link(group[:link], group[:args])
but that put the return value in, and later it is called, returning a method not found error.
但是将返回值放入,后来调用它,返回一个找不到方法的错误。
My current approach has the problem that group
is not in scope within relpath
. I tried also using Proc.new
and lambda
, but since those are not called like normal functions it chokes on them as well.
我目前的方法有一个问题,即组不在relpath范围内。我也试过使用Proc.new和lambda,但由于它们不像普通函数那样被调用,所以它也会扼杀它们。
What can I do to correct this? What is the proper way?
我该怎么做才能纠正这个问题?什么是正确的方法?
EDIT
编辑
If I do
如果我做
suboptions.each do |group|
def relpath(group)
link(group[:link], group[:args]).
end
sub_menu.item group[:name], :path => relpath(group)
end
Then the error I get is:
然后我得到的错误是:
undefined method `mylink?myarg=1' for #<#<Class:0x007fd8068acd58>:0x007fd8068b07f0>
EDIT 2
编辑2
Here is more extensive example of the code.
这是更广泛的代码示例。
menu.item MyTestsController, :text => 'Test', :if => Proc.new { |view| view.can? :read, MyTest } do |sub_menu|
suboptions = [{:link => "tests", :name => "All Systems", :args => {:system_id => 0}},
{:link => "tests", :name => "System A", :args => {:system_id => 1}}]
suboptions.each do |group|
def relpath(group)
link(group[:link], group[:args]).
end
sub_menu.item group[:name], :path => relpath(group)
end
end
1 个解决方案
#1
1
I have no experience with the navigation library in question, but looking at the documentation at https://github.com/rpheath/navigation it seems you are expected to give the name of a Rails named route helper as the path argument - not an actual URL route. The "undefined method" is simply generated because RPH::Navigation tries to call a helper method by the name that is defined in :path argument, and in this case Rails cannot find a named route helper method called "mylink?myarg=1". So basically, what you would need to do is to create a named route, and use that as the path.
我没有相关导航库的经验,但是看看https://github.com/rpheath/navigation上的文档,似乎你应该给出一个名为route helper的Rails名称作为路径参数 - 而不是实际的URL路由。简单地生成“未定义的方法”,因为RPH :: Navigation尝试通过在:path参数中定义的名称调用辅助方法,在这种情况下,Rails无法找到名为“mylink?myarg = 1”的命名路由帮助方法。所以基本上,你需要做的是创建一个命名路由,并将其用作路径。
#1
1
I have no experience with the navigation library in question, but looking at the documentation at https://github.com/rpheath/navigation it seems you are expected to give the name of a Rails named route helper as the path argument - not an actual URL route. The "undefined method" is simply generated because RPH::Navigation tries to call a helper method by the name that is defined in :path argument, and in this case Rails cannot find a named route helper method called "mylink?myarg=1". So basically, what you would need to do is to create a named route, and use that as the path.
我没有相关导航库的经验,但是看看https://github.com/rpheath/navigation上的文档,似乎你应该给出一个名为route helper的Rails名称作为路径参数 - 而不是实际的URL路由。简单地生成“未定义的方法”,因为RPH :: Navigation尝试通过在:path参数中定义的名称调用辅助方法,在这种情况下,Rails无法找到名为“mylink?myarg = 1”的命名路由帮助方法。所以基本上,你需要做的是创建一个命名路由,并将其用作路径。