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

一部のコントローラーでは、私のフォームは正常に動作します。他の人では、次のエラーが発生します。

ID のないアイテムが見つかりませんでした

問題は、定義したメソッドが呼び出されないことです。一部のコントローラーでApplicationHelper.resrouceは呼び出され、他のコントローラーでは他のメソッドリソース (Item.resource ?) であり、どこで定義されているのかわかりません。

メソッド リソースを他の場所でどのように定義できますか? アプリケーション ヘルパーのメソッド リソースが特定のページで呼び出されないのはなぜですか?

: メソッド リソースを定義する gem inherited_resources を使用しています。多分これは紛争を引き起こします。しかし、メソッドがどのように継承されているかわかりません

4

1 に答える 1

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は、そのコントローラーの宝石によって再定義されていました...

于 2013-05-30T08:46:11.337 に答える