0

I am using devise. In a view partial, I have:

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

In my application helper:

  # @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

On some controllers, my form works fine. On others, I get this error:

Couldn't find Item without an ID

The problem is that the method I have defined is not called. With some controllers, ApplicationHelper.resrouce is called, on others, it's an other method resource (Item.resource ?), and I don't know where it's defined.

How the method resource can be defined elsewhere ? Why is the method resource of my application helper not called on certain pages ?

Note: I am using the gem inherited_resources that defines the method resource. Maybe this create the conflict. But I don't know how the method is inherited

Benjamin Crouzier
  • 34,915
  • 36
  • 154
  • 219

1 Answers1

0

After seeing http://railscasts.com/episodes/230-inherited-resources, it all became clear.

I defined resource in 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

  # ...

And now it works. The method resource was being re-defined by the gem for that controller...

Benjamin Crouzier
  • 34,915
  • 36
  • 154
  • 219