14

I have a view for a controller called "show". Inside that view, i want to render the contents of another controller's view - and obviously, the logic for the form on that view to talk to the controller it belongs too. How do I do this? I am fairly new to rails and I'm not 100% confident with the framework yet.

You could almost consider them "widgets" on the view. I know you can render actions from the same controller on the view by using:

render :action => "show_home_page", :layout=> false

but I need it to render the action (view) from another controller.

ie.

I have a view, which is rendered onto a layout. That view, belongs to controller A. I need to render the "index" and "new" views from controller B inside the original view (above)

.

1
  • quote:" I am fairly new to rails and I'm not 100% confident with the framework yet."- I am not new to rails, but when ever I think I understood something, then I am wrong, there is a better solution or a new rails version ;-)
    – halfbit
    Aug 10, 2015 at 18:35

4 Answers 4

34
+100

The terminology in your question is a little confused. If are in a controller and you want to execute the code in another action method in another controller and render its template, you should redirect_to that action. Let's say the other controller is called ContractsController

redirect_to :controller => "contracts", :action => "show_home_page"

If you just want to use the view template from another method as the response from your action, you just need to prefix the name of the controller in the render parameter. This will not call the action, it will just use its template.

For example, if the template lives in the folder for the contracts controller.

render :action => "/contracts/show_home_page", :layout=> false

I think in this case you are actually talking about a partial, which would look like

render :partial => "/contracts/show_home_page"

However, what I see you grasping at here is that you actually want to call multiple action methods to render a single page. This is not how it works. You are going to have to set up the objects that the templates will reference in a single action. This is one reason most Rails developers put a lot of code in the models, so the setup isn't repeated all over the controllers.

But there is another way... where the magic of JavaScript comes in.

In your page, create a function like this:

<script type="text/javascript" language="javascript">    
function load_categories() {
            <%= remote_function(:url => {:controller => "categories", :action => "list"},
                                  :update => "categories")%>
        };
</script>

If you call that in the onload even of your page, it will replace the div with id "categories" with the response from the action referenced.

2
  • 1
    such an amazingly thorough answer. I will digest this for a while and get back to you. Ultimately my hope was to create a set of "widget" like partials, which I could just plonk onto any view I like. The javascript method is something I hadn't even considered.
    – Ash
    Nov 24, 2009 at 5:25
  • unfortunately remote_function no longer works as the prototype helper has been removed in rails 3.1 see we need workarounds like this
    – msanjay
    Jun 18, 2014 at 9:22
0

If you really want to render entirely another action (i.e. simulating a request to go through the entire Ruby on Rails stack), you can use Webrat (check Webrat screencast here).

In traditional MVC web applications, you should rarely have a need to do this. But in my experience developing SOFEA-style and micro-architecture webapps, this is getting more and more common.

1
  • Rarely? This is pretty standard on every blog ever (sidebars are content that often needs it's own controller but then must be rendered alongside multiple different controllers/view) and the problem has inspired gems like cells not to mention a fair number of similar questions here. May 29, 2021 at 20:00
0

I believe the question is asking for the ability to farm out the rendering of something like a sidebar (or repeated form or ..) to it's own controller so one doesn't need to repeat the logic needed to show that view in every controller which uses it.

It has frustrated me since day 1 with rails that there isn't an easy way to simply call an action as if it was a function and have it return the rendered content.

If you want to get fancy there is the cells gem which provides a whole system for inheritance for little ui chunks but it doesn't seem to have been updated recently and it's overkill for most cases. I assume you already know about partials and yield/content_for which are useful in some cases but get messy if you have a bunch of different actions and a bunch of different UI elements that get mixed and match, e.g., a blog with a bunch of types of sidebar that are displayed at random.

There is a great discussion of the various pluses and minuses of the three approaches (partials, yield and cells) in Mauricio Moraes's answer to this question but in practice I think the easiest thing to do is just use javascript to fill that part of the page (form, sidebar whatever) via an XHR.

Still feel there must be some hack that I haven't figured out that lets you treat an action as a function on the server side but no luck yet.

Ohh, and for the simple cases you can probably put the logic into a concern to share it between multiple controllers but that feels wrong for anything complex to me too.

-1

I'm not sure I understand your problem statement fully (why this must be done from the view), but I'd say you just call redirect_to from controller A to controller B.

1
  • ok, so I have a view, which is rendered onto a layout. That view belongs to controller A. I need to render the "index" and "new" views from controller B onto that page.
    – Ash
    Nov 20, 2009 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.