help on before_filter to load resource(s) (current resource) and build resource (current resource) not working...

15 views
Skip to first unread message

satynos

unread,
Dec 17, 2008, 10:07:04 AM12/17/08
to resources_controller
I am trying to move the load resource (s) and build resource code from
actions in the actions.rb to the before filter.

I placed the following code

before_filter(:load_resource, :only =>
[:show, :edit, :update, :destroy])
before_filter(:load_resources, :only => [:index])
before_filter(:build_resource, :only => [:new, :create])

in the resources_controller_for method right after the in-build
before_filter code:

before_filter(:load_enclosing_resources, when_options) unless
load_enclosing_resources_filter_exists?

And also added the following methods to the class InstanceMethods

def load_resource
self.resource = find_resource
end

def load_resources
self.resources = find_resources
end

def build_resource
self.resource = new_resource
end

For some reason, I am getting the following error: "You have a nil
object when you didn't expect it!
You might have expected an instance of Array. The error occurred while
evaluating nil.each"

Looks like @resource and @resouces are not being populated, I even
trie to add attr_accessor :resource, :resources but no luck yet. What
am I doing wrong. Any help in this regards is appreciated.

Thanks in advance.
-Satynos

Chris Hapgood

unread,
Dec 17, 2008, 10:08:58 AM12/17/08
to resources_...@googlegroups.com
You probably need to manually load the resources controller filter
BEFORE your filters. Check the docs -IIRC they show an example of
manually coordinating the sequence of filters.

-Chris
Chris Hapgood
cc...@hapgoods.com




satynos

unread,
Dec 17, 2008, 10:48:18 AM12/17/08
to resources_controller
Chris:

I didn't quite understand what you mean by manually load the resources
controller filter BEFORE my filters. I tried to debug to see the
problem (by throwing in some RAILS_DEFAULT_LOGGER.info calls), for
some reason, in-built resources_controller filter
'load_enclosing_resources' is called but my filters are never
executed. If I could get my filters executed, I could dig into the
problem. I still don't understand why my filters are never called
where as the other filter is called all the time, even though I
specified my filters the same way.


-Satynos
> c...@hapgoods.com

satynos

unread,
Dec 17, 2008, 10:55:41 AM12/17/08
to resources_controller
Chris:

When I move my filters into my controller, they are being executed. I
dont understand why they are not being executed when I keep the same
within the resources_controller. Is Rails going to execture only one
before_filter from a given plugin?

Chris Hapgood

unread,
Dec 17, 2008, 11:06:52 AM12/17/08
to resources_...@googlegroups.com
In the simplest case, you should be able to do this in any one
controller (like ApplicationController):

before_filter :load_enclosing_resource
before_filter :my_filter_that_depends_on_resources_being_identified

I haven't tested this lately, but it was working for me six months ago
in another app.

Also, the ordering of filters when inheriting from
ApplicationController, for example, and especially when mixing before
and around filters, is best learned by reading the Rails docs. The
ordering rules are not trivial.

If your filters are not being executed that is usually a sign that a
prior filter has halted the execution -look in the logs.

-Chris



If you split these two filters across
Chris Hapgood
cc...@hapgoods.com




satynos

unread,
Dec 17, 2008, 11:22:53 AM12/17/08
to resources_controller
My filters are being executed when I place them either in
ApplicationController or my own controllers that derive from
ApplicationController.

Interesting thing is they are not being executed when I place them
within the resources_controller_for method in resources_controller.rb
even though the in-built filter load_enclosing_resources is being
executed. I also checked the logs and there is no problem. I even
checked the AgileWeb book and Rails API about the filters, and nothing
specified about only one filter being executed within the plugin.

I will try to do some more debugging and try to get back to you.

-Satynos
> c...@hapgoods.com

satynos

unread,
Dec 17, 2008, 1:02:41 PM12/17/08
to resources_controller
Chris:

My bad. My changes in the method resources_controller_for were not
picked up until I restart the server. Once I restart the server
everything work fine as I intended. Another interesting observatino
is, although I changed all the requrie to require_dependency so that I
can user confi.reload_plugins = true in environment.rb so that my
changes will reflect immediately without restarting the server, any
change to the files except init.rb and resources_controller.rb were
being picked up. I dont quite understand why the changes in those two
files were not picked up. I understand why init.rb as rails loads only
once these files, but dont understand why resources_controller.rb
file. Trying to investigate it with some refactoring, will let you
know my findings, unless you already know and would like to share and
save me some time. :)

-Satynos
Reply all
Reply to author
Forward
0 new messages