【问题标题】:issue with delete URL when using rails 4使用 rails 4 时删除 URL 的问题
【发布时间】: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


【解决方案1】:

您生成的 HTML、路由和控制器看起来都不错。听起来您的应用程序和浏览器中启用了正确的 js,但在您的日志中,您的请求是 GET 而不是 DELETE。这使我相信您的 javascript 中的其他地方一定有错误,这会阻止页面上的所有 javascript 执行。我推荐firebug 来定位错误。

【讨论】:

  • 项目中缺少我的 JS 文件 - 它们不在供应商目录中 我应该使用什么命令来确保将 jquery 和 jquery_uj 添加到那里
  • 你不需要在那里添加它们,通常来自 gems 的 js 可以从 gem 本身访问,而无需你的任何干预。
  • 不幸的是,即使我有 jquery-rails gem(我尝试再次安装),也不能解决问题。还有其他想法吗?谢谢
  • 如果其他的都100%正确,那基本上一定是js错误。
  • 没有一个 JS 文件被提供给浏览器——它们都失败了
【解决方案2】:

你能在 crome 浏览器中重现这个吗?我的意思是它是特定于浏览器的还是无处不在?

你可以清理你的资产并再次预编译

rake assets:clean
rake assets:precompile 

【讨论】:

  • 在 chrome 和 firefox 中测试。我还运行了 rake assets:clean 和 rake assets:precompile 但这并没有解决它。
  • 我的问题是:我需要手动复制 jquery 文件还是将它们作为任何命令的结果在 vendor/assets/javascript 目录中创建?
  • 你不需要对jquery-rails gem 附带的 jquery 文件进行查询
  • 它们会出现在供应商目录中吗?因为他们现在不在。我的 Gemfile 有 jquery-rails
  • 它不会出现在供应商目录中,因为它由 jquery-rails 引擎提供服务
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
相关资源
最近更新 更多