【问题标题】:table row as remote data link表行作为远程数据链接
【发布时间】:2015-11-20 11:55:53
【问题描述】:

我的代码现在是这样的:

_index.html.erb

<tr class="clickable" data-link=<%= edit_patient_report_path(@patient, report) %> data-remote="true"></tr>

edit.js.erb

$('#report-index').hide(); // #report-index => container with all reports
$('#report-form').html("<%= escape_javascript(render 'form') %>").show(); // #report-form => container for report form

report.js.coffee

$ ->
  $(document).on 'click','.clickable', (event) ->
    target = $(event.target)
    if target.is(':not(a)')
      if $(this).attr('data-link')
        window.location.href = $(this).data('link')
      else
        ...

reports_controller.rb

class ReportsController < ApplicationController
  respond_to :js, :html
  ...
  def edit
  end
  ...
end

当我点击表格行时,它会重定向到编辑表单。但是由于没有可用的相应模板,它会引发 模板丢失 错误:

缺少模板报告/编辑、使用 {:locale=>[:de]、:formats=>[:html]} 应用/编辑,...

我可以将此行设为可点击的远程链接吗?

(没有在它通过的行内实现某种隐藏链接)

更新

感谢@user3506853 帮助我解决了我的问题:

report.js.coffee(更新)

$ ->
  $(document).on 'click','.clickable', (event) ->
    target = $(event.target)
    if target.is(':not(a)')
      if $(this).attr('data-link')
        $.ajax({
          dataType: 'script',
          url: $(this).data('link')
        })
      else
        ...

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    如果您希望 tr 可点击,则必须使用 js 代码,如下所示:-

    <tr data-href="<%= edit_patient_report_path(@patient, report) %>"></tr>
    
    $(document).on('click', '#table-id tr', function() {
        var link = $(this).data('href');
         $.ajax({
             type: 'PUT',
             url: link
         });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多