开发者

invoke method on rails3 view hepler problem

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  


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜