【发布时间】:2018-08-03 04:48:20
【问题描述】:
我有一个主页,其中包含一个表格中的软件列表。
通过此链接,我以模式打开我的软件页面:
<% = link_to software_path (software), remote: true do %> ... <% end%>
我创建了一个脚本,允许我在单击带有 .clickable 类的 td 时打开我的模态,而当我单击带有 .no-click 类的 td 时不打开模态。
$(".clickable").click(function(e) {
if (!$(e.target).hasClass('no-click')) {
$('#software-modal').modal('show');
}
});
结构:
_software.html.erb 打开_software_modal.html.erb
问题: 当我尝试通过脚本打开我的模态时,它总是打开相同的模态。也就是点击软件2,软件3,...打开软件1的模态,直接用链接就没有问题了。
所以我的脚本中有 id 或类似问题...
你能帮帮我吗?
编辑 1:根据 VAD 的响应,我尝试使用 data-id 搜索 id。
_software.html.erb
<tr class="clickable" data-id="<%= software.id %>">
...
</tr>
<div id='software-content'></div>
_software_modal.html.erb
<div id="software-modal" class="modal fade" role="dialog">
...
</div>
show.js.erb
$('#software-content').html("<%= j render 'software_modal', software: @software %>");
$('#software-modal').modal('show');
这个 show.js.erb 允许我通过示例链接打开我的模式:
<%= link_to software.name, software_path(software), remote: true,class:"no-click" %>
但不是通过可点击的表格。
所以我在 _software.html.erb 中添加了一个脚本,使我的表格可点击:
$(".clickable[data-id]").click(function(e) {
if (!$(e.target).hasClass('no-click')) {
window.location = '/softwares/' + $(this).attr('data-id');
}
});
我也尝试使用数据链接,...有没有办法在模式中打开这个 window.location(如链接)?我昨天搜索了几个小时,但我没有找到任何东西..
编辑 2
根据@VAD 的响应,渲染布局:false,我们可以输入:
class ApplicationController < ActionController::Base
layout proc{|c| c.request.xhr? ? false : "application" }
end
允许对 js 进行虚假渲染,对 html 进行真实渲染。所以要让我的 html 页面与我的起始布局保持一致。
谢谢@dogenpunk Render without layout when format is JS (needs drying)
【问题讨论】:
-
更新了我的答案,看看吧
标签: ruby-on-rails