Tristan
2010-Mar-01 03:11 UTC
Rendering an action (with layout) into a Javascript template
Hi everyone I have an action: def index @settings = Setting.all(:order => ''name'') respond_to do |format| format.html format.js end end and corresponding views: index.html.erb: <h1>Settings</h1> ... index.js.erb $("<%= escape_javascript(render XXX) %>").bigDialog(); So what''s happening is that for HTML requests the HTML template will be rendered (inside application.html.erb) and for Javascript requests I want the HTML template to be rendered as a string into the Javascript snippet so that jQuery can throw out a dialog. The catch is that dialogs have a separate layout file, dialog.html.erb. Is there a way to render a template with a layout into my Javascript snippet where I''ve typed "render XXX"? I''ve looked at render_to_string but that breaks MVC by taking view material into the controller. Ideally, I''d like something like render(:action => ''index.html.erb'', :layout => ''dialog'') (note index.html.erb to distinguish it from index.js.erb) and have this rendered inline. Ideas? Thanks! Tristan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Mar-01 12:27 UTC
Re: Rendering an action (with layout) into a Javascript template
On Mar 1, 3:11 am, Tristan <tristanmchar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there a way to render a template with a layout into my Javascript > snippet where I''ve typed "render XXX"? I''ve looked at render_to_string > but that breaks MVC by taking view material into the controller. > > Ideally, I''d like something like render(:action => > ''index.html.erb'', :layout => ''dialog'') (note index.html.erb to > distinguish it from index.js.erb) and have this rendered inline.I think either render :template, or render :action => ..., :format => ''html'' would do this Fred> > Ideas? > > Thanks! > Tristan-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tristan
2010-Mar-01 19:27 UTC
Re: Rendering an action (with layout) into a Javascript template
Thanks for the answer Fred. After reading the docs for ActionView::Base.render (http://apidock.com/ rails/ActionView/Base/render) I found that ActionView''s render is quite different to ActionController''s render. Notably, there aren''t options for :template or :action. However, there is an option for :file and you can also provide a :layout option which I''ve done: $("<%= escape_javascript(render(:file => ''settings/ index.html.erb'', :layout => ''layouts/dialog'')) %>").bigDialog(); This is works very well but it''s long and not very agile with all those full paths. Going to use this for now but I''m going to keep searching for a more robust solution. T. On Mar 2, 1:27 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 1, 3:11 am, Tristan <tristanmchar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Is there a way to render a template with a layout into my Javascript > > snippet where I''ve typed "render XXX"? I''ve looked at render_to_string > > but that breaks MVC by taking view material into the controller. > > > Ideally, I''d like something like render(:action => > > ''index.html.erb'', :layout => ''dialog'') (note index.html.erb to > > distinguish it from index.js.erb) and have this rendered inline. > > I think either render :template, or render :action => ..., :format => > ''html'' would do this > > Fred > > > > > > > Ideas? > > > Thanks! > > Tristan-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.