SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    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.

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Shouldn't you put that array-returning method into a helper object?

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any link to helper object tutorial?

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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.

  5. #5
    SitePoint Member
    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.

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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.

  7. #7
    SitePoint Addict
    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.

  8. #8
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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
    Then you can use link_to to generate the proper links:
    Code:
    <% link_to :controller => 'admin/project', :action => 'edit' %>
    For end-user actions you can create your controllers like you normally would, or you can subclass those too:
    Code:
    ruby script/generate controller EndUser::Project

  9. #9
    SitePoint Addict
    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.

  10. #10
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mixins wouldn't be more flexible?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •