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!!