I have a controller (employee) with it''s model and view. Now I need to write another controller (emp) which would not have any model or view. This emp controller should be able to invoke all the methods of employee controller.And eventually be able to represent all the relevant information. I have also a dependent controller of ''employee'' which is ''account'' . I have set up routes as per that. # in routes.rb ----------------------------------------- resources :employees do resources :accounts end and, resources :emp, :as =>"employees" do resources :acc , :as=>"accounts" end ------------------------------------------ POST on /emp/ would be as good as a POST on /employees/ or GET on /emp/1/edit should work as GET on /employees/1/edit or GET on /emp/1/acc should be equivalent to GET on /employees/1/ accounts I was thinking how to write the emp controller.Please help. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2012-Apr-02 12:21 UTC
Re: Invoking one controller''s methods from other controller
On 2 April 2012 11:17, Pallav_bfs <spallav88-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a controller (employee) with it''s model and view. Now I need to > write another controller (emp) which would not have any model or view. > This emp controller should be able to invoke all the methods of > employee controller.And eventually be able to represent all the > relevant information.Why have you posted this again rather than continuing the previous thread that you started? You cannot call one controllers methods from another (or at least you should not). Either extract the common code to a library or don''t use a separate controller. To use the existing controller add parameters to the existing actions if required or add new methods, then if the URL is important to you use routing to provide the URLs you require. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jeffrey L. Taylor
2012-Apr-02 16:11 UTC
Re: Invoking one controller''s methods from other controller
Quoting Pallav_bfs <spallav88-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> I have a controller (employee) with it''s model and view. Now I need to > write another controller (emp) which would not have any model or view. > This emp controller should be able to invoke all the methods of > employee controller.And eventually be able to represent all the > relevant information. > > I have also a dependent controller of ''employee'' which is ''account'' . > I have set up routes as per that. > # in routes.rb > ----------------------------------------- > resources :employees do > resources :accounts > end > > and, resources :emp, :as =>"employees" do > resources :acc , :as=>"accounts" > end > ------------------------------------------ > POST on /emp/ would be as good as a POST on /employees/ > or GET on /emp/1/edit should work as GET on /employees/1/edit > or GET on /emp/1/acc should be equivalent to GET on /employees/1/ > accounts > > I was thinking how to write the emp controller.Please help. >You''ve said what is the same between emp and employee. So what is different between emp and employee? In general, you cannot call the instance methods of one class for another class, only class methods. Depending on what you are trying to do, emp can inherit from employee or there is only the employee controller and routes map the emp requests onto the employer controller. HTH, Jeffrey -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.