0

I am unable to delete a record in ruby on rails for some reason. I have tried many solutions unfortunately to no avail. (such as How to delete / destroy a record in rails?)

The contents of my application.erb are:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

when i open in html it only displays the following css it does not display js as in the tags above.

<link data-turbolinks-track="true" href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/statuses.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />

for my index.html.erb

 <h1>Listing statuses</h1>

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Content</th>
          <th colspan="3"></th>
        </tr>
      </thead>

      <tbody>
        <% @statuses.each do |status| %>
          <tr>
            <td><%= status.name %></td>
            <td><%= status.content %></td>
            <td><%= status.id %></td>
            <td><%= link_to 'Show', status %></td>
            <td><%= link_to 'Edit', edit_status_path(status) %></td>
            <td><%= link_to 'Destroy', status , method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
        <% end %>
      </tbody>
    </table>

    <br>

    <%= link_to 'New Status', new_status_path %>

i am troubleshooting this currently to no avail, if someone can pick any faults in this code, i would greatly appreciate. :) the js files are not showing in the head section of the page and the destroy function is not working, let alone the confirm tag.

Update: Now I am getting this error:

TypeError: Object doesn't support this property or method Rails.root: [location]

if i remove the '<%= javascript_include_tag %>' it functions, although not fully (ie. does not delete like in the first instance)

update 2 when i click on destroy button, it simply redirects me to view that particular status only, it does not destroy

update 3 inside the application.js i have the following:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

update 4 I have changed the <%= javascript_include_tag %> to <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %> and it loads the js file however when i click on that js file from the sourcecode i get a routing error. Any possible way i can solve that?

Community
  • 1
  • 1
  • How does it not work? What happens instead? – janfoeh Apr 08 '15 at 10:35
  • plz show me your log contents, after clicked on the destroy action. – Uday kumar das Apr 08 '15 at 10:39
  • Look in your log. Is the right action being called? Look at the params being passed through. Do they match what is expected by the code in the action? Step through the code in the action, with the params. What happens when you save the object? Does save fail, and return false? If so, are there validation failures? If any of these questions reveal something unexpected happening, you can try to work out why. That's your strategy. – Max Williams Apr 08 '15 at 10:39
  • 2
    It looks you don't include any javascript code in tag `javascript_include_tag` it is necessary for sending proper DELETE request via link `` tag. – edariedl Apr 08 '15 at 10:53
  • Hate to ask the obvious, but where is your controller code? – Matt Stevens Apr 08 '15 at 13:47
  • @Udaykumardas: how can i get the log contents. I am a newbie still trying to learn ruby on rails. edariedl: i figured that out that the javascript is absent, i dont know why it happens since it already has the javascipt include tag. @ Matt As to the controller code where can i find it? – user3108623 Apr 13 '15 at 07:17
  • for getting js logs you should `F12` key in the browser and the select `Network` and the click on the destroy and send me the error logs. – Uday kumar das Apr 13 '15 at 07:20

1 Answers1

0

Inside application.erb, you need to include your application.js file with javascript_include_tag like this:

<%= javascript_include_tag 'application' %>

Assuming you have jquery and jquery_ujs set inside your application.js

karlingen
  • 13,800
  • 5
  • 43
  • 74
  • indeed, they are included, however none of the javascript is shown in html only the css files – user3108623 Apr 14 '15 at 07:19
  • @user3108623 do you have `therubyracer` inside your Gemfile? – karlingen Apr 14 '15 at 08:23
  • i have and it is commented out. – user3108623 Apr 15 '15 at 08:56
  • Uncomment it, run `bundle install` and reload the server. Check if you have the same problems. – karlingen Apr 15 '15 at 09:50
  • i ran it. the problem does not seem to be therubyracer per se. i now get a javascript file in head but when i click on it i get: `Routing Error: No route matches [GET] "/javascripts/default.js"` – user3108623 Apr 15 '15 at 11:58
  • @user3108623 change `<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>` to `<%= javascript_include_tag :default, 'data-turbolinks-track' => true %>` and you should be good to go. – karlingen Apr 15 '15 at 13:05