How to popup a panel when clicking a URL in rails?

不想你离开。 提交于 2019-12-06 15:55:40

问题


I'm new to rails and I need to know how to pop-up a panel by clicking a URL ?? Please can some one give me solution for this problem.


回答1:


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/




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7266822/how-to-popup-a-panel-when-clicking-a-url-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!