【发布时间】:2015-09-13 07:49:18
【问题描述】:
我正在使用 rails v4.2.4。我无法让模型“发布”的删除功能正常工作。相关日志、配置和代码复制如下:
浏览器中的 HTML
<td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/1">Destroy</a></td>
我在销毁行中尝试了@post、post & posts_path(post) 和posts_path(@path),但没有成功。 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'
后控制器
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
日志文件
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)
我也尝试将我的 IP 地址列入白名单,但这并没有解决问题。呈现的 HTML 文件中的删除 URL 应该是“/posts/1/delete”还是“/posts/1/destroy”或其他?如何让帖子的销毁功能正常工作? JS 在浏览器上启用。
更新 application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
宝石文件
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 所有 JS 文件都会出现 503 服务错误。
<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>
问题已解决: 这个问题是由于我在 Rails 前面运行的代理服务器配置错误引起的。此代理将静态资产的流量路由到不正确的地址,导致 JS 文件无法被拾取。修复此问题后,删除/销毁功能正常工作!!
【问题讨论】:
-
你能用
application.js更新你的问题吗? -
它有 jquery 和 jquery_usj 导入。更新帖子
-
我可以看到 javascript 文件没有被下载。当我创建项目时,这些文件默认没有添加到供应商目录中的我的rails项目中(我在创建rails项目时没有传入skip js标签)。我已经运行了捆绑安装。我应该手动复制这些文件(它们位于服务器上)
-
你能在 crom 浏览器中重现这个吗?我的意思是它是特定于浏览器的还是无处不在?
-
您真的应该考虑将您的解决方案添加为答案,而不是将其编辑到您的问题中。虽然,有些人可能会争辩说你的解决方案是“你的 javascript 中的一个错误,它阻止了页面上的所有 javascript 执行”,但谁会预测到......
标签: ruby-on-rails ruby ruby-on-rails-4