【发布时间】:2013-05-03 08:06:14
【问题描述】:
这个论坛上的问题似乎都没有解决我的具体需求。基本上,我有一个“详细信息”按钮。我希望它在单击时显示一个模式对话框,其中包含从模型的 show.html.erb 中提取的信息。 我有一个 book.rb 模型。在我的索引页面中:
<div class="detail_button"><%= link_to "Details", book %></div>
单击此按钮通常会将我带到 book/id 页面,使用 show 操作。但我不希望它离开页面,而是想要一个可以关闭的模式弹出窗口。我已经尝试了这个论坛中相关主题的所有 jquery 和 javascript 代码,但似乎没有一个能奏效。大多数似乎只针对创建或自定义操作。
为了避免重复,我尝试了以下方法,但都没有奏效:
这个:
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.
$('.modal').dialog(); //jquery ui will open it as a modal popup
这个:
$('a[data-popup]').live('click', function(e) {
window.open($(this).attr('href'));
e.preventDefault();
});
还有这个:
$('a[data-popup]').live('click', function(e) { window.open($(this).attr('href')); e.preventDefault(); });
= link_to( 'Create a new company', new_company_path, 'data-popup' => true )
有帮助吗?这里的菜鸟。
【问题讨论】:
-
第一个代码示例看起来应该可以工作。当您单击第一个示例的链接时会发生什么?请务必使用 Google Chrome、Firebug 等调试 JavaScript。
标签: jquery ruby-on-rails jquery-ui ruby-on-rails-3.2 popup