Forum: Ruby on Rails Render :action of a different controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
59bbe3780ec28c45b4945f00435efeb3?d=identicon&s=25 Nikos Dimitrakopoulos (demisone)
on 2007-05-10 00:57
(Received via mailing list)
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 :)

-Nikos
6d5602ac22cc57505c0d3a24b3d63108?d=identicon&s=25 Tyler Prete (Guest)
on 2007-05-10 01:10
(Received via mailing list)
Yes, I'm fairly sure you can do that, exactly like your example,
actually.

--Tyler Prete
A73f446eaaf56db0aeaa578aa373989b?d=identicon&s=25 Terry Donaghe (Guest)
on 2007-05-10 05:17
(Received via mailing list)
Yup - I've got that all over some of my own code on a site running right
now.  :)

On 5/9/07, Tyler Prete <psyonic@gmail.com> wrote:
> > I just want to know if and how i can use this method for rendering an
> >
>
> >
>


--
Terry (TAD) Donaghe
http://tadspot.tumblr.com
59bbe3780ec28c45b4945f00435efeb3?d=identicon&s=25 Nikos Dimitrakopoulos (demisone)
on 2007-05-10 13:53
(Received via mailing list)
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 :) 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 :)

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 :) ***

Waiting for your thoughts/comments/suggestions :)

-Nikos
A73f446eaaf56db0aeaa578aa373989b?d=identicon&s=25 Terry Donaghe (Guest)
on 2007-05-10 23:49
(Received via mailing list)
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. <demisone@gmail.com> wrote:
> class GoalsController < ApplicationController
>     end
> feeling lazy is:
>         end
>     end
> Waiting for your thoughts/comments/suggestions :)
> >
> > > > on the parameters passed to the render method when using the
> >
> > --
> > Terry (TAD) Donaghehttp://tadspot.tumblr.com
>
>
> >
>


--
Terry (TAD) Donaghe
http://tadspot.tumblr.com
Ede5ca2f61280a8bb5adc31bbd828ab7?d=identicon&s=25 Khaled Hassounah (Guest)
on 2007-05-11 01:50
(Received via mailing list)
> 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 :) ***

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

Khaled
59bbe3780ec28c45b4945f00435efeb3?d=identicon&s=25 Nikos Dimitrakopoulos (demisone)
on 2007-05-11 09:01
(Received via mailing list)
Yeah, i think this makes sense :)

Thanks a lot Khaled
This topic is locked and can not be replied to.