[Rails] Re: How to "intercept" each action call?
@Jeremy:
#1 seems to be the easiest, but #2 is the best solution.
Here's my view of how this can work:
class TasksController < ApplicationController
# Specify which class has the generic scaffolding methods
scaffold :my_custom_scaffold_class
# or we bundle a generic class with the gem
scaffold
# Overriding is possible
def show(id)
# instance variable is already injected by our gem
# do something with the @task
end
end
or even
class ApplicationController < ActionController::Base
scaffold
end
to propagate to all controllers which inherit from ApplicationController.
Dheeraj Kumar
On Saturday 12 May 2012 at 6:49 AM, Dheeraj Kumar wrote:
I understand. See the link I posted, where the OP does exactly your requirement. Here's some code:class ApplicationController < ActionController::Baseinclude MyGemendclass TasksController < ApplicationControllerbefore_filter :find_taskdef find_task@task = Task.find(params[:id])# Some other operation involving MyGem if you need...enddef showrespond_with(@task)endendDheeraj KumarOn Saturday 12 May 2012 at 6:37 AM, Lauro Caetano wrote:
I'm trying to do this:class ApplicationController < ActionController::Baseinclude MyGemendclass TasksController < ApplicationControllerinjector_to :taskdef showrespond_with(@task)endendWhen the action 'show' is called, the "injector_to" should inject the @task for me.In other words, injector_to makes "Task.find(params[id])" for me in this case.Other example: If I have this action ->def createrespond_with(@task)endthe injector_to should do the hard work, instantiating the @task and saving it.--I think that I've explained better now.2012/5/11 Jeremy Walker <jez.walker@gmail.com>On 12 May 2012 01:48, Lauro Caetano <laurocaetano1@gmail.com> wrote:Thanks for your help Dheeraj, but I'm trying to do something like Responders.Responders has a respond_to :html in controllers, that is used in each action call to respond_with.The thing is: call a specific method that is located inside my gem for every action call. This method should be able to modify the attribute that was passed.I have no idea to make this work.Can you give an example of this working, please? I don't understand what you mean by "the attribute that was passed". What modifications is it going to make?
2012/5/11 Dheeraj Kumar <a.dheeraj.kumar@gmail.com>Dheeraj KumarOn Saturday 12 May 2012 at 5:53 AM, Lauro Caetano wrote:
--Hi!I'm a RoR beginner and I'm trying to make a gem that can "intercepts" all action from my controllers.I would like to do something like that:class ApplicationController < ActionController::Baseinclude MyGemendclass MyController < ApplicationControllerdo_something_to :some_attributedef indexrespond_with(@some_attribute)endendIn each call of index or other action from MyController, I want that MyGem execute "do_something_to" modifying the value of "some_attribute". Is it possible?Thanks,Lauro CaetanoYou received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home