How to popup a panel when clicking a URL in rails?
I'm new to rails and I need to know how to pop-up a pane开发者_运维技巧l by clicking a URL ?? Please can some one give me solution for this problem.
You could look at modal dialogs by www.jqueryui.com. Add jquery ui to your application.
Put a hidden div (display:none) in your layout page.
<div class="modal" style="display:none;">
</div>
Your link should be an ajax link:
<%= link_to 'Link', events_path(@event), :remote => true %>
Your controller should accept ajax response:
def show
@event = Event.find(params[:id])
respond_to do |format|
format.js
end
end
This is where the magic happens. After pressing the link via ajax, your show.js file will insert content into your empty hidden div and display the popup. Your views should have a javascript file: /view/events/show.js.erb
$('.modal').html("<%= escape_javascript(render(@event)) %>"); //inserts content into your empty div, be aware that the parameter needed to be quoted.
$('.modal').dialog(); //jquery ui will open it as a modal popup
In the example above it will render an event partial. So you should make it by creating an _event.html.erb file in /views/events/
Take a look at Shadowbox, Colorbox or Lightbox.
Code example using Shadowbox:
in your head tag:
<%= stylesheet_link_tag "shadowbox" %>
in the application layout (before closing your body tag):
<%= javascript_include_tag "shadowbox.js" %>
<script type="text/javascript">
Shadowbox.init({
handleOversize: "drag",
overlayColor: '#fff'
});
</script>
And in your views:
link_to(event.name, event_path(event), :rel => 'shadowbox;width=500;height=300;')
Note that you might need to change some of the code above, depending on where you extracted the Shadowbox source files. Hope that helps.
Opening new window isn't actually rails responsibility. You should do it by using javascript snippet. Consider this:
<script type="text/javascript">
window.open('desired/URL', 'windowName', 'height=200,width=200');
</script>
you should suit the third argument to your needs. Look at: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
Notice, that there is no problem with using rails snippets in js (<%= %>), so you can make link flexible by using some helper method.
精彩评论