Forum: Ruby on Rails render js from another 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.
Ec854a864bcefae4d8068d0f4dcbc705?d=identicon&s=25 Tarscher (Guest)
on 2009-02-13 13:11
(Received via mailing list)
Hi all,

In my GamesController I try to render create_or_update.js that is
stored in the events view dir. Apparently Rails alwayslooks in the
games view dir.

Is there a way I can force Rails to look in the events view dir?

class GamesController < ApplicationController
  def create
    @event = Game.new(params[:game])
    respond_to do |format|
      if @event.save
        format.js { render :action => '../events/create_or_update' }
#doesn't work
      end
    end
  end
end

Thanks
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-02-13 18:18
Tarscher wrote:
> Hi all,
>
> In my GamesController I try to render create_or_update.js that is
> stored in the events view dir. Apparently Rails alwayslooks in the
> games view dir.
>
> Is there a way I can force Rails to look in the events view dir?
>
> class GamesController < ApplicationController
>   def create
>     @event = Game.new(params[:game])
>     respond_to do |format|
>       if @event.save
>         format.js { render :action => '../events/create_or_update' }
> #doesn't work
>       end
>     end
>   end
> end
>
> Thanks

Excerpt from Rail docs on the render method
-------------------------------------------
Rendering a template

Template rendering works just like action rendering except that it takes
a path relative to the template root. The current layout is
automatically applied.
  # Renders the template located in
[TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails,
app/views/weblog/show.erb)
  render :template => "weblog/show"

  # Renders the template with a local variable
  render :template => "weblog/show", :locals => {:customer =>
Customer.new}
-------------------------------------------

>         format.js { render :template => 'events/create_or_update' }

I think this is the right way to do it.
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-02-13 18:19
Robert Walker wrote:
>>         format.js { render :template => 'events/create_or_update' }
>
> I think this is the right way to do it.

On second thought maybe that should be
         format.js { render :template => 'events/create_or_update',
:layout => false }
This topic is locked and can not be replied to.