Is it possible to default layout to false for all ajax requests?

6 views
Skip to first unread message

George Adamson

unread,
Dec 14, 2009, 10:49:21 AM12/14/09
to merb
Instead of checking request.ajax (or request.xhr) before deciding
whether to set layout=false, is it possible to have this happen
automatically? Is there somewhere I can set this more generically to
apply to all actions?

I only ask because it seems like a lot of repeated code to be testing
in every controller action?

Cheers,

George

Nicholas Orr

unread,
Dec 14, 2009, 5:14:45 PM12/14/09
to merb
use a before filter?

2009/12/15 George Adamson <George....@softwareunity.com>

--

You received this message because you are subscribed to the Google Groups "merb" group.
To post to this group, send email to me...@googlegroups.com.
To unsubscribe from this group, send email to merb+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/merb?hl=en.



刘松

unread,
Dec 14, 2009, 8:43:40 PM12/14/09
to me...@googlegroups.com
It need change "how to find the default layout if not specified by render method" in merb.
By now, it only search layout by inheritance order?

Use a before filter will work, but not as good.

I have no more suggestion because I know less about merb's code.

George Adamson

unread,
Dec 16, 2009, 12:08:56 PM12/16/09
to merb
I'm trying the following but it does not seem to have any effect:

class Notes < Application
before Proc.new { layout = false if request.ajax? }
...

I must be missing something?!
Cheers,
George


On Dec 15, 1:43 am, 刘松 <liusong1...@gmail.com> wrote:
> It need change "how to find the default layout if not specified by render
> method" in merb.
> By now, it only search layout by inheritance order?
>
> Use a before filter will work, but not as good.
>
> I have no more suggestion because I know less about merb's code.
>
> On Tue, Dec 15, 2009 at 6:14 AM, Nicholas Orr <nicholas....@zxgen.net>wrote:
>
> > use a before filter?
>
> > 2009/12/15 George Adamson <George.Adam...@softwareunity.com>
>
> > Instead of checking request.ajax (or request.xhr) before deciding
> >> whether to set layout=false, is it possible to have this happen
> >> automatically? Is there somewhere I can set this more generically to
> >> apply to all actions?
>
> >> I only ask because it seems like a lot of repeated code to be testing
> >> in every controller action?
>
> >> Cheers,
>
> >> George
>
> >> --
>
> >> You received this message because you are subscribed to the Google Groups
> >> "merb" group.
> >> To post to this group, send email to me...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> merb+uns...@googlegroups.com <merb%2Bunsu...@googlegroups.com>.
> >> For more options, visit this group at
> >>http://groups.google.com/group/merb?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "merb" group.
> > To post to this group, send email to me...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > merb+uns...@googlegroups.com <merb%2Bunsu...@googlegroups.com>.

Yehuda Katz

unread,
Dec 16, 2009, 12:33:16 PM12/16/09
to me...@googlegroups.com
Oh noes. Trapped by the Ruby local variable ambiguity. You'll need self.layout=, as layout = defines a local in the proc's scope.
 
Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325


To unsubscribe from this group, send email to merb+uns...@googlegroups.com.

George Adamson

unread,
Dec 17, 2009, 4:27:43 AM12/17/09
to merb
Thanks Yehuda, I tried it with self.layout=false but apparently the
layout method does not exist:
"undefined local variable or method `layout=' for #<Notes:0x1c98360> -
(NameError)"

Is there something wrong with my syntax?

Here's the controller code:
(I'm hoping to replace the commented 'request.ajax' conditions with
one 'before' filter but the before filter causes the 'undefined' error
on any action call)

class Notes < Application

before Proc.new{ self.layout = false if request.ajax? }

def index
@notes = Note.all
display @notes #, request.ajax? ? { :layout=>false } : nil
end

def edit(id)
only_provides :html
@note = Note.get(id)
raise NotFound unless @note
display @note #, request.ajax? ? { :layout=>false } : nil
end

...etc...
end

George

On Dec 16, 5:33 pm, Yehuda Katz <wyc...@gmail.com> wrote:
> Oh noes. Trapped by the Ruby local variable ambiguity. You'll need
> self.layout=, as layout = defines a local in the proc's scope.
>
> Yehuda Katz
> Developer | Engine Yard
> (ph) 718.877.1325
>
> On Wed, Dec 16, 2009 at 12:08 PM, George Adamson <
>

> > merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>


> > >.
> > > >> For more options, visit this group at
> > > >>http://groups.google.com/group/merb?hl=en.
>
> > > >  --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "merb" group.
> > > > To post to this group, send email to me...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > merb+uns...@googlegroups.com<merb%2Bunsu...@googlegroups.com><

> > merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>

jonah honeyman

unread,
Dec 17, 2009, 5:21:45 AM12/17/09
to me...@googlegroups.com
You need to use 'layout(false)' not layout=false

To unsubscribe from this group, send email to merb+uns...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/merb?hl=en.





--
-jonah

jonah honeyman

unread,
Dec 17, 2009, 5:32:23 AM12/17/09
to me...@googlegroups.com
Also, the before filter is run on the instance of the controller, while :layout is a class method. Your before filter should look like this:

before Proc.new{ self.class.layout(false) if request.ajax? }
--
-jonah

George Adamson

unread,
Dec 17, 2009, 6:57:03 AM12/17/09
to merb
Ah that did the trick, thanks Jonah and everyone!

The solution is to use:


before Proc.new{ self.class.layout(false) if request.ajax? }

Much tidier than lots of conditions littering the controller actions.

George

On Dec 17, 10:32 am, jonah honeyman <gewglestolemyp...@gmail.com>
wrote:

> >> > > merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
> >> <merb%252Buns...@googlegroups.com<merb%25252Bun...@googlegroups.com>


>
> >> > > >.
> >> > > > >> For more options, visit this group at
> >> > > > >>http://groups.google.com/group/merb?hl=en.
>
> >> > > > >  --
> >> > > > > You received this message because you are subscribed to the Google
> >> > > Groups
> >> > > > > "merb" group.
> >> > > > > To post to this group, send email to me...@googlegroups.com.
> >> > > > > To unsubscribe from this group, send email to
> >> > > > > merb+uns...@googlegroups.com<merb%2Bunsu...@googlegroups.com>
> >> <merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
> >> ><

> >> > > merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
> >> <merb%252Buns...@googlegroups.com<merb%25252Bun...@googlegroups.com>


>
> >> > > >.
> >> > > > > For more options, visit this group at
> >> > > > >http://groups.google.com/group/merb?hl=en.
>
> >> > > --
>
> >> > > You received this message because you are subscribed to the Google
> >> Groups
> >> > > "merb" group.
> >> > > To post to this group, send email to me...@googlegroups.com.
> >> > > To unsubscribe from this group, send email to
> >> > > merb+uns...@googlegroups.com<merb%2Bunsu...@googlegroups.com><
> >> merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
> >> >.
> >> > > For more options, visit this group at
> >> > >http://groups.google.com/group/merb?hl=en.
>
> >> --
>
> >> You received this message because you are subscribed to the Google Groups
> >> "merb" group.
> >> To post to this group, send email to me...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> merb+uns...@googlegroups.com <merb%2Bunsu...@googlegroups.com>.
> >> For more options, visit this group at
> >>http://groups.google.com/group/merb?hl=en.
>
> > --

> > -jonah
>
> --
> -jonah

George Adamson

unread,
Dec 17, 2009, 7:15:36 AM12/17/09
to merb
Oh no! I take it back...
This solution disabled layout for *all* requests to the controller,
even those where request.ajax==false:

before Proc.new{ self.class.layout(false) if request.ajax? }
(Presumably it is not working because it is setting an attribute on
the class and not the instance?)

The intention is to disable layout for any action when
request.ajax==true.

Anyone else got a suggestion please?!

I found this related post but it suffers from the same 'undefined
method' error described earlier:
http://groups.google.com/group/merb/browse_thread/thread/4b1d52a5e0061da5/09121030ffd93c65?lnk=gst&q=layout&pli=1
George


On Dec 17, 11:57 am, George Adamson <George.Adam...@SoftwareUnity.com>
wrote:

jonah honeyman

unread,
Dec 17, 2009, 8:22:47 AM12/17/09
to me...@googlegroups.com
Use a touch of logic in the before hook:

before Proc.new{ self.class.layout(request.ajax? ? false : :application) }

To unsubscribe from this group, send email to merb+uns...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/merb?hl=en.





--
-jonah

George Adamson

unread,
Dec 18, 2009, 4:02:01 AM12/18/09
to merb
Phew, that worked. Thank you so much Jonah!
George


On Dec 17, 1:22 pm, jonah honeyman <gewglestolemyp...@gmail.com>
wrote:


> Use a touch of logic in the before hook:
>
> before Proc.new{ self.class.layout(request.ajax? ? false : :application) }
>
> On Thu, Dec 17, 2009 at 2:15 PM, George Adamson <
>
>
>
> George.Adam...@softwareunity.com> wrote:
> > Oh no! I take it back...
> > This solution disabled layout for *all* requests to the controller,
> > even those where request.ajax==false:
> >  before Proc.new{ self.class.layout(false) if request.ajax? }
> > (Presumably it is not working because it is setting an attribute on
> > the class and not the instance?)
>
> > The intention is to disable layout for any action when
> > request.ajax==true.
>
> > Anyone else got a suggestion please?!
>
> > I found this related post but it suffers from the same 'undefined
> > method' error described earlier:
>

> >http://groups.google.com/group/merb/browse_thread/thread/4b1d52a5e006...

> > > > >> > > merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
> > <merb%252Buns...@googlegroups.com<merb%25252Bun...@googlegroups.com>
>
> > > > >> <merb%252Buns...@googlegroups.com<merb%25252Bun...@googlegroups.com>
> > <merb%25252Bun...@googlegroups.com<merb%2525252Bu...@googlegroups.com>


>
> > > > >> > > >.
> > > > >> > > > >> For more options, visit this group at
> > > > >> > > > >>http://groups.google.com/group/merb?hl=en.
>
> > > > >> > > > >  --
> > > > >> > > > > You received this message because you are subscribed to the
> > Google
> > > > >> > > Groups
> > > > >> > > > > "merb" group.
> > > > >> > > > > To post to this group, send email to me...@googlegroups.com.
> > > > >> > > > > To unsubscribe from this group, send email to
> > > > >> > > > > merb+uns...@googlegroups.com<merb%2Bunsu...@googlegroups.com>
> > <merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
>
> > > > >> <merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
> > <merb%252Buns...@googlegroups.com<merb%25252Bun...@googlegroups.com>
>
> > > > >> ><

> > > > >> > > merb%2Bunsu...@googlegroups.com<merb%252Buns...@googlegroups.com>
> > <merb%252Buns...@googlegroups.com<merb%25252Bun...@googlegroups.com>
>
> > > > >> <merb%252Buns...@googlegroups.com<merb%25252Bun...@googlegroups.com>
> > <merb%25252Bun...@googlegroups.com<merb%2525252Bu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages