SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
-
Mar 7, 2006, 08:00 #1
- Join Date
- Feb 2006
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How to call action in another controller?
Hi,
I am confused on how to call an action in another controller? I looked into render_component and render_component_as_string but neither does what I am looking for. I want an action to return array so I can use the returned array in another controller. I also do not want it to render. I searched google but I couldn't find any example that fits my problem.
-
Mar 7, 2006, 08:55 #2
Shouldn't you put that array-returning method into a helper object?
-
Mar 7, 2006, 09:18 #3
- Join Date
- Feb 2006
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
any link to helper object tutorial?
-
Mar 7, 2006, 09:40 #4
Actually, my mistake in terminology. That array-returning method should probably go in your ApplicationController, which would make it a global method (look in /app/controllers/application.rb for ApplicationController). Helpers are meant for common view functions.
-
Mar 7, 2006, 10:05 #5
- Join Date
- Feb 2006
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So there is no way to access another controller's action unless i make them global by putting them in application controller in RoR? Wouldn't that hurt modularity of the code? I can imagine application controller become bloated and becoming one big include page that I used to seeing in php. There has to be a way to call another action in another controller?
Maybe I am wording my problem wrong, but there has to be a simple way of doing this.
-
Mar 7, 2006, 10:27 #6
If you need a method shared between multiple controllers, then the ApplicationController is where it should go. I see no "bloat" in this because putting a method in one place is better than copying that same method into multiple controllers. Now if you need a method that's specific to a model, then put the method in that model and call that model from your controller.
Controllers are meant to route and redirect, as well as to glue models and views together. If you need to chain multiple controller actions together, then forward or redirect to another controller/action when the first is done processing.
-
Mar 7, 2006, 13:30 #7
- Join Date
- Nov 2001
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I was planning to call an action on another controller for my site admin section, but from your comments I wonder if that's not the best way to do it, so I'd like some advice please.
Let's say I have controllers for Customer, Project and Admin. Customer and Project have administrative actions such as new and edit as well as end-user actions such as list and view.
My plan was to use the Admin controller to provide the administrative interface, so the administrator is presented with links to do Customer => new, Customer => edit, Project => new and Project => edit.
I know I can manually create links like /Customer/new, but is there any way to use link_to for creating these links? Is there a better way to implement the admin interface?
Thanks.
-
Mar 7, 2006, 13:35 #8
I'd subclass the administrative portions of Customer and Project under Admin.
Code:ruby script/generate controller Admin::Customer ruby script/generate controller Admin::Project
Code:<% link_to :controller => 'admin/project', :action => 'edit' %>
Code:ruby script/generate controller EndUser::Project
-
Mar 7, 2006, 13:57 #9
- Join Date
- Nov 2001
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for your help, it's just what I was looking for.
-
Mar 9, 2006, 09:27 #10
- Join Date
- Jun 2004
- Location
- France
- Posts
- 129
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Mixins wouldn't be more flexible?
Bookmarks