Topic: render... some way

How do I call an action and it's associated template that's in a different controller from a view, ie, I want the html produced by a 'resources' controller's list function to display in the view of my projects show action.  Can I pass the action an argument?  Is there a better way to do what I want according to rails and MVC?

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: render... some way

I'm confused, do you want to render part of a view inside another view? You can do this with partials. If you want to make a partial accessible by all controllers, place it in a folder outside of the controller's view folder, something like "/app/views/shared/_partial.rhtml" and then access it like:

<%= render :partial => 'shared/partial' %>

Railscasts - Free Ruby on Rails Screencasts

Re: render... some way

I think I don't understand the use of partials properly.  I wanted to insert the result of calling /somecontroller/someaction into a different controllers view.  What's more if the action that I was trying to call has an argument, how would I pass in the argument?  I have a funny feeling that if I wrote my code right I should never have to do that.  I'm going to explore a _partial solution... HA!  sorry...

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: render... some way

Yeah, I'd say you probably should re-think how you're approaching this.  Actually calling an action requires a request and all that jazz, and wasn't designed for inline rendering.  You should make the business logic sharable, and put the view logic in a partial as ryan suggested.

Re: render... some way

I have done this quite a bit in my app.  For example, when editing one object I might want to have a table of all its children.  It's simply a matter of:

render(:partial => "thing/list_each", :collection => @things)

where list_each is the partial that I have already created for 'thing' and @things was calculated in the parent controller.  I've never aksed myself whether this is the right way to do it but it certainly works.

Re: render... some way

Understood.  Thanks for your help.

For those that will look at this in the future, always remember that if something is oddly complicated or difficult chances are you are going about it all wrong... or you are using the wrong language.  Here's a helpful link to do things better on a general scale.

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)