2013-05-30 25 views
0

我正在使用設計。在視圖部分,我有:設計:方法資源沒有正確調用

<%= simple_form_for resource, :as => :user, ... 

在我的應用助手:

# @see http://stackoverflow.com/questions/4541075/what-is-the-devise-mapping-variable-and-how-can-i-include-it 
    def resource_class 
    devise_mapping.to 
    end 

    # @see http://stackoverflow.com/questions/4081744/devise-form-within-a-different-controller 
    def resource_name 
    :user 
    end 

    def resource 
    # with some controllers (homepage, static, this method is called) 
    # with other controllers (Item, it's not called: I don't see the abort call) 
    abort('should stop here') 
    @resource ||= User.new 
    end 

    def devise_mapping 
    @devise_mapping ||= Devise.mappings[:user] 
    end 

在有些控制器上,我的表單工作正常。在其他人,我得到這個錯誤:

Couldn't find Item without an ID

問題是,我已定義的方法不會被調用。對於某些控制器,ApplicationHelper.resrouce被調用,在其他情況下,它是另一種方法資源(Item.resource?),我不知道它在哪裏定義。

如何在別處定義方法資源?爲什麼我的應用程序幫助程序的方法資源沒有在某些頁面上調用?

說明:我正在使用定義方法資源的gem inherited_resources。也許這會造成衝突。但我不知道如何繼承方法

+0

相關:https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form -anywhere - 在你的應用程序 –

回答

0

看到http://railscasts.com/episodes/230-inherited-resources後,這一切都變得清晰。

我在上述ItemsController定義的資源:

class ItemsController < InheritedResources::Base 

    # @see http://stackoverflow.com/questions/16831095/devise-the-method-resource-is-not-called-properly 
    def resource 

    # this solves my problem. It looks super wrong tough 
    @resource ||= User.new 

    #view_context.resource # tried that, but doesn't work 
    end 

    # ... 

而現在它的工作原理。方法resource正在由該控制器的寶石重新定義...