Render :action of a different controller


#1

Hi, it may be trivial, but i couldn’t find any [clear] documentation
on the parameters passed to the render method when using the :action.
I just want to know if and how i can use this method for rendering an
action’s layout that belongs to a different controller. Something like
“render :action => list, :controller => blog” for example…

Thanks a lot in advance :slight_smile:

-Nikos


#2

Yes, I’m fairly sure you can do that, exactly like your example,
actually.

–Tyler P.


#3

Yup - I’ve got that all over some of my own code on a site running right
now. :slight_smile:

On 5/9/07, Tyler P. removed_email_address@domain.invalid wrote:

I just want to know if and how i can use this method for rendering an


Terry (TAD) Donaghe


#4

For some reason my previous mail didn’t show up here… grrrr i hate
it when i have to rewrite something that’s long enough to take me 15
minutes to write again :slight_smile: Anyway here it goes…

I tried what you said that worked for you so i could verify it and
also add some hints on the documentation of ActionController::Base but
didn’t quite worked for me… Here’s what i tried:

class GoalsController < ApplicationController
def short_goal
end
def long_goal
end
end

class FoulsController < ApplicationController
def penalty
if true
render :action => ‘short_goal’, :controller => ‘goals’
end
end
end

and the views accordingly… The result when hitting fouls/penalty was
“Template missing. Missing template ./script/…/config/…/app/views/
FOULS/short_goal.rhtml”

After reading the source for ActionController::Base#render which calls
ActionController::Base#render_action when :action is passed i thought
that this result makes sense (at least to me). The source if you’re
feeling lazy is:

  def render_action(action_name, status = nil, with_layout = true)

#:nodoc:
template = default_template_name(action_name.to_s)
if with_layout && !template_exempt_from_layout?(template)
render_with_layout(:file => template, :status =>
status, :use_full_path => true, :layout => true)
else
render_without_layout(:file => template, :status =>
status, :use_full_path => true)
end
end

where “action_name” is the value of :action

So i tried the following:

class FoulsController < ApplicationController
def penalty
if true
render :action => ‘…/goals/short_goal’
end
end
end

and worked. The thing is that i don’t think this is a ‘nice’ way of
doing this - the smell of bugs is close :slight_smile:

Also how can i call the action before rendering the view to set up the
variables. *** I know i should probably do a redirect instead all this
fuzz but curiosity is driving me at the moment :slight_smile: ***

Waiting for your thoughts/comments/suggestions :slight_smile:

-Nikos


#5

Also how can i call the action before rendering the view to set up the
variables. *** I know i should probably do a redirect instead all this
fuzz but curiosity is driving me at the moment :slight_smile: ***

I think you’re looking for render_component (http://
api.rubyonrails.org/classes/ActionController/Components.html)

Khaled


#6

Yeah, i think this makes sense :slight_smile:

Thanks a lot Khaled


#7

Hi Nikos,

I looked at this a bit - I was thinking of redirect:.

def penalty
redirect_to :controller => ‘goals’, :action => ‘short_goal’
end

I guess that makes more sense because what your code is doing is taking
the
user to a totally different page than the penalty.rhtml page that you
ought
to go to when fouls.penalty is called.

On 5/10/07, Nikos D. removed_email_address@domain.invalid wrote:

class GoalsController < ApplicationController
end
feeling lazy is:
end
end
Waiting for your thoughts/comments/suggestions :slight_smile:

on the parameters passed to the render method when using the


Terry (TAD) Donaghehttp://tadspot.tumblr.com


Terry (TAD) Donaghe