views:

564

answers:

4

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)

.

A: 

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.

JRL
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
+1  A: 

Look into shared partials with locals.

glebm
+2  A: 

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.

MattMcKnight
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
A: 

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.

Hendy Irawan