views:

14

answers:

1

i have two view helper

module Admin::CategoriesHelper
    def test
       return "a"
    end


module CategoriesHelper
    def test
       return "b"
    end

i invoke test method in views/admin/categories/index.html.erb

====================================================================

if i use Admin::CategoriesHelper.test it will throw error like bellows:

NoMethodError in Admin/categories#index
Showing /home/mlzboy/my/b2c2/app/views/admin/categories/index.html.erb where line #32 raised:

undefined method `my_new_admin_category_path' for Admin::CategoriesHelper:Module
Extracted source (around line #32):

29: 
30: <br />
31: 
32: <%= link_to 'New Category', Admin::CategoriesHelper.my_new_admin_category_path(@parent) %>

it's return b not a

if i change the method name like test2 didn't have the same with CategoriesHelper it's work fine

so how to resolve this problem,i newibe in rails,i want to know why this happen,thanks

is there something wrong with my routes.rb?,my routes.rb file is as bellows

 namespace :admin do
    resources :categories
 end
 resources :categories  
A: 

Both your Admin::CategoriesHelper and CategoriesHelper are loaded and exposed in your views. This means that the module that was loaded last will be executed.

To illustrate my point, your view loads helpers like this:

class MyView
  include Helper1
  include Helper2
end

Both Helper1 and Helper2 include a method test. In this case when you run MyView#test it will execute Helper2#test. Helper1#test has been overwritten and cannot be accessed.

Because ActionView (or whatever is responsible for loading the helpers) loads helpers in an arbitrary order, you're in trouble.

My solution is to prefix methods in the namespaced module:

module CategoriesHelper
  def test
  end
end

module Admin::CategoriesHelper
  def admin_test
  end
end
Ariejan
i have renamed to admin_test,and it can invoke by admin_test,why it can't by Admin::CategoriesHelper.admin_test?
mlzboy
also i have another relevent question is <%= link_to 'Show', admin_category %> it output http://0.0.0.0:3000/categories/7but i hope is http://0.0.0.0:3000/admin/categories/7,what's wrong?
mlzboy