1

disclaimer I am pretty new to 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:

  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.

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.

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

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 
3
  • :path => :relpath? Should not it be :path => relpath? Commented Oct 17, 2012 at 6:11
  • what is the sub_menu class? And the API of item() Seems you are using the function the wrong way. Commented Oct 17, 2012 at 6:17
  • I am not familiar with this library but I would play around trying to set :path as a controller action name and maybe adding a :params => args to the item function. Commented Oct 17, 2012 at 6:58

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.