表中的行是链接但删除链接不起作用

时间:2014-03-18 07:01:26

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

在本主题中:

Making a table row into a link in Rails 我发现解决方案在表中有一行是一个链接。但是这个解决方案没有什么问题。 我的index.html.haml:

%table.table.table-striped.table-bordered#car_configurations
  %thead
    %tr
      %th{width: "10%"}=t('car_configurations.car_configuration.photo')
      %th=t('car_configurations.car_configuration.brand')
      %th=t('car_configurations.car_configuration.model')
      %th=t('car_configurations.car_configuration.body_style')
      %th=t('car_configurations.car_configuration.car_class')
      %th=t('car_configurations.car_configuration.cases')
      %th
  %tbody
    - @car_configurations.each do |car_configuration|
      %tr{"data-link" => edit_admin_car_configuration_path(car_configuration)}
        %td= image_tag car_configuration.image_url(:thumb).to_s
        %td= car_configuration.brand.name
        %td= car_configuration.model.name
        %td= car_configuration.body_style.name
        %td= car_configuration.car_class.name
        %td= cases(car_configuration)
        %td
          = link_to ('x'), admin_car_configuration_path(car_configuration), method: :delete, data: {confirm: "Jesteś pewien"}, class: "delete"
= link_to t('car_configurations.index.add'), { action: :new }, class: "btn btn-primary"

在我的js.coffee文件中,我有:

$("tr[data-link]").click ->
    window.location = @dataset.link

问题是当我点击我的删除链接片刻显示一个javascript popoup然后它将我重定向到编辑路径,我无法删除对象。对此有什么解决方案吗?

编辑:

感谢您的帮助。现在,destroy链接正在运行,但当我点击表格中的行时,它会将我重定向到:

http://localhost:3000/admin/undefined 

而不是:

http://localhost:3000/admin/car_configuration/1/edit

我的index.html.haml如下所示:

%table.table.table-striped.table-bordered#car_configurations
  %thead
    %tr
      %th{width: "10%"}=t('car_configurations.car_configuration.photo')
      %th=t('car_configurations.car_configuration.brand')
      %th=t('car_configurations.car_configuration.model')
      %th=t('car_configurations.car_configuration.body_style')
      %th=t('car_configurations.car_configuration.car_class')
      %th=t('car_configurations.car_configuration.cases')
      %th
  %tbody
    - @car_configurations.each do |car_configuration|
      %tr{"data-link" => edit_admin_car_configuration_path(car_configuration)}
        %td= image_tag car_configuration.image_url(:thumb).to_s
        %td= car_configuration.brand.name
        %td= car_configuration.model.name
        %td= car_configuration.body_style.name
        %td= car_configuration.car_class.name
        %td= cases(car_configuration)
        %td{class: "delete"}=link_to ('x'), admin_car_configuration_path(car_configuration), method: :delete, data: {confirm: "Jesteś pewien"}
= link_to t('car_configurations.index.add'), { action: :new }, class: "btn btn-primary"

1 个答案:

答案 0 :(得分:1)

你的JS几乎肯定是这里的问题

1. You're using a Ruby variable in JS
2. You're only sending one "link" request (can't be edit & delete)

我会做这样的事情:

 %td{class: "delete"}=
   link_to ('x'), admin_car_configuration_path(car_configuration), method: :delete, data: {confirm: "Jesteś pewien"}

$("tr[data-link]").on "click", "td:not(.delete)", ->
    window.location = $(this).parent().data("link")