Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a view of another controller

What I want to do is have 2 different controllers, client and test_client. The client controller is already built and I want to create a test_client controller that i can use to play around with the UI of the client and adjust it as needed. I am mainly trying to get around the validation i have built into the client and its dependence on an admin controller that loads the data.

so i want the test_client controller to load a sample data set and then render the client controller's index view so i can adjust the clients UI. That is all.

I tried this in the test_clients index method:

class TestClient
    def index
        render :template => 'client/index'
    end
end

but i get an error because it cannot find the client partials as it is looking in the current controllers view for them...

So I have looked into this already and most people say that you should never make such a call but i think that this case is a reasonable usage...I just need to figure out how to get it to work.

like image 505
german129 Avatar asked Dec 01 '11 17:12

german129


People also ask

What is the difference between view rendering and controller rendering?

A Sitecore Controller Rendering is more complex than a View Rendering, mainly in that it requires a controller action. The rendering definition item includes both a controller name and controller action to execute. The controller action is then responsible for returning the correct view. A basic controller rendering method.

How to display a view from another controller in MVC?

In this article, we will explain how to display a view from another controller in ASP.NET MVC with an example and sample code. In this example, we have created a sample login page, and based on Login we are redirecting to another controller action method. Open Visual Studio. Click on the file in the menu and select new Project .

What is controller rendering in Sitecore?

A Controller Rendering definition item. A Sitecore Controller Rendering is more complex than a View Rendering, mainly in that it requires a controller action. The rendering definition item includes both a controller name and controller action to execute. The controller action is then responsible for returning the correct view.

How do I share a view with multiple controllers?

By default, ASP.NET MVC checks first in Views [Controller_Dir], but after that, if it doesn't find the view, it checks in ViewsShared. The shared directory is there specifically to share Views across multiple controllers. Just add your View to the Shared subdirectory and you're good to go.


1 Answers

You will need to adjust your view so that the path to the partial you need is in the form 'controller/partial'. In this case probably 'client/partial'. Then you can simply use render 'client/index' as before.

So say somewhere in your view you have this:

<%= render :partial => 'info' %>

You will want to change it to this:

<%= render :partial => 'client/info' %>
like image 169
Mario Avatar answered Sep 28 '22 09:09

Mario