0

In this topic:

Making a table row into a link in Rails I found solution to have a row in table that is a link. But with this solution is little problem. My 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"

In my js.coffee file I have:

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

The problem is that when I click my delete link for a moment shows a javascript popoup and then it redirect me to the edit path and I can't delete object. Is there any solution for that?

EDIT:

Thank's for help. Now the destroy link is working but when i click row in table it redirect me to:

http://localhost:3000/admin/undefined 

rather than:

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

My index.html.haml looks like this:

%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"
Community
  • 1
  • 1
Mateusz Urbański
  • 7,352
  • 15
  • 68
  • 133
  • 3
    Well the Javascript is the problem. You can't both a) redirect to an edit page and b) go to the delete page. Pick one. – sevenseacat Mar 18 '14 at 07:04
  • 1
    post some jQuery code which you tried. – Jai Mar 18 '14 at 07:04
  • My jquery code is in my post. So there is no solution to this problem? – Mateusz Urbański Mar 18 '14 at 07:09
  • 1
    You do not have pure Js in your code. `-> window.location = @dataset.link` is not JavaScript. Anyway, use a link with an HREF to go to where you want and then redirect. There is no reason to use JavaScript to change location if you need to do something on the server too – mplungjan Mar 18 '14 at 07:14
  • Yes because I'm using coffescript. Sorry but I still don't know how to resolve this problem... – Mateusz Urbański Mar 18 '14 at 07:23
  • 1
    Depends on what you want. If you want to popup something, then do the redirect in the callback from the popup buttons – mplungjan Mar 18 '14 at 08:42
  • Thank's everyone for help, I decided to not use this feature because it's complicate something that must be simple. – Mateusz Urbański Mar 18 '14 at 08:54

1 Answers1

1

Your JS is almost certainly the problem here

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

I would do something like this:

 %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")
Richard Peck
  • 76,116
  • 9
  • 93
  • 147