I have a rest endpoing that allows my users to create a new subscriber by posting to the subscribers collection, e.g.: [POST] sevice/subscribers Although, this is fine for outsiders using the API, I need have an internal app that needs to call the API too. Actually, it''s in the same project at /subscription. It''s just a signup form which posts to the subscribers collection. And since it''s in the same project I would like to "over ride" the create method of my subscribers_controller. For example, a method called create_subscriber in subscription_controller (my signup form). In create_subscriber I would like to post to the subscribers collection like previously, but I would also like to do some additional processing after it comes back successful. class SubscriptionController < ApplicationController def index # render page... end def create_subscriber #post to subscribers collection with @subscriber Subscriber.new(params[:subscriber]) # do additional processing end end Here''s the problem. There''s duplication there, the subscriptions controller has the same exact code to create a new subscriber. How can I just call the SubscriptionsController.create to remove this duplication? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Wang
2007-Sep-06 04:44 UTC
Re: Calling a controller method from another controller
eggie5 wrote:> I have a rest endpoing that allows my users to create a new subscriber > by posting to the subscribers collection, e.g.: > > [POST] sevice/subscribers > > Although, this is fine for outsiders using the API, I need have an > internal app that needs to call the API too. Actually, it''s in the > same project at /subscription. It''s just a signup form which posts to > the subscribers collection. And since it''s in the same project I would > like to "over ride" the create method of my subscribers_controller. > > For example, a method called create_subscriber in > subscription_controller (my signup form). In create_subscriber I would > like to post to the subscribers collection like previously, but I > would also like to do some additional processing after it comes back > successful. > > class SubscriptionController < ApplicationController > > > def index > # render page... > end > > def create_subscriber > #post to subscribers collection with @subscriber > Subscriber.new(params[:subscriber]) > # do additional processing > end > > end > > Here''s the problem. There''s duplication there, the subscriptions > controller has the same exact code to create a new subscriber. How can > I just call the SubscriptionsController.create to remove this > duplication?You can move that method into application.rb which is the parent class of your controllers. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---