Open a (redbox) modal popup from inside a controller?

28 views
Skip to first unread message

athem

unread,
Jan 19, 2008, 3:54:05 PM1/19/08
to Ruby on Rails: Talk
Hi,

I'm trying to open a modal popup window using redbox to display a
styled notification/alert to the user. The helpers in the redbox
documentation only describe how to open a redbox when the user clicks
on a link, e.g., link_to_redbox() or link_to_remote_redbox().

Is there any way to open a redbox, or a different type rails-
compatible modal popup, from inside a controller action?

Thanks.

Phil Tayo

unread,
Jan 20, 2008, 11:50:55 AM1/20/08
to rubyonra...@googlegroups.com
ermmm I'm a newb but I think you should look into the redbox.js file and
see if you can call some of the functions in there directly instead of
via link_to_redbox calls....activateRBWindow might be a useful one?


--
Posted via http://www.ruby-forum.com/.

Phil Tayo

unread,
Jan 20, 2008, 11:52:20 AM1/20/08
to rubyonra...@googlegroups.com
Phil Tayo wrote:
> ermmm I'm a newb but I think you should look into the redbox.js file and
> see if you can call some of the functions in there directly instead of
> via link_to_redbox calls....activateRBWindow might be a useful one?

also maybe you can help me out with my problem...do you know how to get
the redbox to appear in the centre of the page? at the moment it seems
stuck to the top of the window...

tonypm

unread,
Jan 20, 2008, 12:12:11 PM1/20/08
to Ruby on Rails: Talk
This interested me. There may be better modal solutions than redbox
for your need, but if you are already using redbox then you may want
to use it. The easiest way I found was to create a hidden div in the
normal view.

eg a div with id='my_box'
Set the div style to display: none

It doesnt matter where it is positioned because redBox clones it

In the controller I am assuming that you are already responding to an
ajax call so in the
render : update

add
page << "RedBox.showInline('my_box')"

There may be some bits and pieces to tidy, but that worked for me.

hth
Tonypm

tonypm

unread,
Jan 20, 2008, 12:21:16 PM1/20/08
to Ruby on Rails: Talk
Phil wrote:

> also maybe you can help me out with my problem...do you know how to get
> the redbox to appear in the centre of the page? at the moment it seems
> stuck to the top of the window...

In the redbox.js file (in public/javascripts)
in setOverlaySize: function()
I have done this. (addition of one line of code)

// added by APM to account for using absolute positioning in my
content.
// for some reason, the above calculations dont give a large enough
height. It is
// only the size of the banner.
// When the scrollbars appear on the windon, then all is ok. This is
because the first
// option innerHeight+scrollMaxY is used.
if (yScroll < window.innerHeight)
{
yScroll=window.innerHeight //Added by APM
}

$("RB_overlay").style['height'] = yScroll +"px";
},

I believe that there are some other solutions in the comments on the
web page for redbox, (version 2). I have not worked with redbox for a
while, so I do not have it at my fingertips. There is also something
I recall about using position:fixed which also helps position
calculations.

hth
Tonypm

Anton J Aylward

unread,
Jan 20, 2008, 7:59:52 PM1/20/08
to rubyonra...@googlegroups.com
tonypm said the following on 20/01/08 12:12 PM:

>
> In the controller I am assuming that you are already responding to an
> ajax call so in the
> render : update
>
> add
> page << "RedBox.showInline('my_box')"
>
> There may be some bits and pieces to tidy, but that worked for me.

I don't think that's a valid assumption.
I can envision a whole pile of applications where view, delete, hoojum,
snarf and many other methods in the controller trigger the redbox..
As in

...
record = Page.find(:first, ... param[ whatever ] ..)
begin
if ! record.can_do(action, current_user) then
if current_user.nil? then
Redbox.login("You need to be logged in")
retry
end
Redbox.alert("You don't have permission to #{action} this record")
# don't redisplay anything.
end
...
end

You can argue that things like the 'edit' button should be grayed out
and inoperative if the can_do for it is negative. I can argue that it
shouldn't be and that the controller should respond with a login prompt.
Ultimately its up to the guy paying for the application to decide on the
policy. But I, and I think others, would like to know how to handle it
in the controller pretty much the way I've sketched out and to have
anything in the 'update' or 'render'. Access control issues should be
dealt with earlier.

--
"Cutting the space budget really restores my faith in humanity. It
eliminates dreams, goals, and ideals and lets us get straight to the
business of hate, debauchery, and self-annihilation." -- Johnny Hart

Reply all
Reply to author
Forward
0 new messages