0

I am using rails v4.2.4. I have had trouble getting the delete functionality for a model "Post" to work correctly. The relevant logs, configs and code are copied below:

HTML in browser

<td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/1">Destroy</a></td>

I have tried @post, post & posts_path(post) and posts_path(@path) in the destroy line without success. index.html.erb

 <tbody>
    <% @posts.each do |post| %>
      <tr>
        <td><%= post.title %></td>
        <td><%= post.body %></td>
        <td><%= link_to 'Show', post %></td>
        <td><%= link_to 'Edit', edit_post_path(post) %></td>
        <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Ar
e you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>

routes.rb

Rails.application.routes.draw do
  resources :posts
 # get 'posts/index'

  # The priority is based upon order of creation: first created -> highest pri
ority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
# I commented out the two lines below after adding them when troubleshooting
 #  delete 'posts' => 'posts#destroy'
 #  root 'posts#index'

Post controller

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy
   # flash[:success] = "Post deleted"
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

Log file

Started GET "/posts/4" for x.x.x.x at 2015-09-13 03:39:15 -0400
Processing by PostsController#show as HTML
  Parameters: {"id"=>"4"}
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 4]]
  Rendered posts/show.html.erb within layouts/application (0.9ms)

I have also tried white listing my IP address but that has not solved the issue. Should the delete URL in the rendered HTML file be "/posts/1/delete" or "/posts/1/destroy" or something else? How do I get the destroy functionality for a post to work? JS is enabled on the browser.

Update application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

Gemfile

gem 'jquery-rails'

application.rb

Rails.application.configure do

# Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end

HTML rendered on browser All JS files get a 503 service error.

 <script src="/assets/jquery.self-a714331225dda820228db323939889f149aec0127aeb06255646b616ba1ca419.js?body=1"></script>
<script src="/assets/jquery_ujs.self-d456baa54c1fa6be2ec3711f0a72ddf7a5b2f34a6b4f515f33767d6207b7d4b3.js?body=1"></script>
<script src="/assets/turbolinks.self-c37727e9bd6b2735da5c311aa83fead54ed0be6cc8bd9a65309e9c5abe2cbfff.js?body=1"></script>
<script src="/assets/application.self-3b8dabdc891efe46b9a144b400ad69e37d7e5876bdc39dee783419a69d7ca819.js?body=1"></script>
  <script src="/assets/jquery.self-a714331225dda820228db323939889f149aec0127aeb06255646b616ba1ca419.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/jquery_ujs.self-d456baa54c1fa6be2ec3711f0a72ddf7a5b2f34a6b4f515f33767d6207b7d4b3.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/turbolinks.self-c37727e9bd6b2735da5c311aa83fead54ed0be6cc8bd9a65309e9c5abe2cbfff.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/application.self-3b8dabdc891efe46b9a144b400ad69e37d7e5876bdc39dee783419a69d7ca819.js?body=1" data-turbolinks-track="true"></script>
  <meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="2z8rPBN8GkxpFVpQjDK+Bnd2fX/FSYG23VdVX+FxdvlOsa2ggi60psjDNQ31Y05FDbXKIQyM3WLhh19BCuGRGA==" />
</head>

Issue resolved: The issue was caused by my misconfiguration of a proxy server running in front of Rails. This proxy was routing the traffic for static assets to an incorrect address causing the JS files to not get picked up. After fixing this issue, the delete/destroy functionality is working!!

user3813256
  • 792
  • 1
  • 6
  • 19
  • Can you update your question with `application.js`? – Pavan Sep 13 '15 at 07:54
  • it has the jquery and jquery_usj imports. Updating post – user3813256 Sep 13 '15 at 07:57
  • I can see that the javascript files are not being downloaded. When I created the project, these files were not added by default to my rails project in the vendors directory (i did not pass in skip js tags when creating the rails project). I have ran bundle install. Should I be copying these files by hand (they are located on the server) – user3813256 Sep 13 '15 at 08:00
  • Can you reproduce this in crome browser? I mean it is browser specific or it occurs everywhere? – Vishnu Atrai Sep 13 '15 at 08:01
  • I have tested in both chrome and firefox – user3813256 Sep 13 '15 at 08:04
  • In logs it should looks like: Started DELETE "/posts/4". Same url, different method. Looks like jquery_ujs is not loaded. – Kirill Platonov Sep 13 '15 at 08:05
  • @KirillPlatonov spaciba Kirill - my question is this: do I need to run any command to get the jquery files inside the vendor/assets/javascript directory (I have already ran the bundle install and rake assets:clean/precompile commands). The jquery and jquery_ujs references are in my application.js file – user3813256 Sep 13 '15 at 08:09
  • This is a bit of a reach, but is your application.js included in the page? – Brad Werth Sep 13 '15 at 08:10
  • possible duplicate of [Delete link sends "Get" instead of "Delete" in Rails 3 view](http://stackoverflow.com/questions/3774925/delete-link-sends-get-instead-of-delete-in-rails-3-view) – Brad Werth Sep 13 '15 at 08:13
  • I have seen that question - it does not solve my issue – user3813256 Sep 13 '15 at 08:14
  • thank you for answering - I really appreciate the effort. I solved the issue and will update the post soon. It was my fault - another server component was misconfigured. – user3813256 Sep 13 '15 at 08:33
  • 1
    You should really consider adding your solution as an answer, rather than editing it into your question. Although, some might argue that your solution was "an error elsewhere in your javascript which is preventing all of the javascript on the page from executing", but who would have predicted that... – Brad Werth Sep 13 '15 at 08:37

2 Answers2

1

Can you reproduce this in crome browser? I mean it is browser specific or it occurs everywhere?

You can clean your assets and precompile again

rake assets:clean
rake assets:precompile 
Vishnu Atrai
  • 2,370
  • 22
  • 24
1

Your generated HTML, routes, and controller all look good. It sounds like the proper js is enabled in your app and in your browser, but in your logs your request is a GET instead of a DELETE. This leads me to believe that you must have an error elsewhere in your javascript which is preventing all of the javascript on the page from executing. I recommend firebug for locating the error.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • my JS files are missing from the project - they are not in the vendors directory What command should I use to ensure that jquery and jquery_uj gets added there – user3813256 Sep 13 '15 at 08:10
  • You don't need to add them there, usually js from gems is accessible from the gem itself without any intervention on your part. – Brad Werth Sep 13 '15 at 08:12
  • unfortunately, even though I have jquery-rails gem (I tried installing again), that does not solve the issue. Any other ideas? thanks – user3813256 Sep 13 '15 at 08:13
  • If you have everything else 100% correct, it basically has to be a js error. – Brad Werth Sep 13 '15 at 08:14
  • None of the JS files get served to the browser - they all fail – user3813256 Sep 13 '15 at 08:15
  • Are you running in production mode without compiling assets, or have you turned off server_static_assets in your development.rb or application.rb while in development mode? – Brad Werth Sep 13 '15 at 08:17
  • I copied the development.rb file. rails console loads development environment. I am not specifying any other environment – user3813256 Sep 13 '15 at 08:23
  • So the js files get added into the html, are requested by the browser, and return a 404? – Brad Werth Sep 13 '15 at 08:27
  • thank you for answering - I really appreciate the effort. I solved the issue and will update the post soon. It was my fault - another server component was misconfigured. – user3813256 Sep 13 '15 at 08:33