无法获取“删除”链接以在Ruby on Rails中的管理面板上工作

时间:2018-10-03 23:07:52

标签: ruby-on-rails ruby methods view

我是Ruby On Rails的新手,目前正在为在应用程序内手动构建的管理面板创建标准的CRUD操作(不使用管理工具)。

但是,当我在自己的show.html.erb页面上调用删除链接时,没有任何反应(错误屏幕或浏览器中没有任何其他反馈)。 Javascript已完全启用,我相信我已经安装了正确的gem。

有人可以让我知道解决此问题的最佳方法吗?我将在下面链接所有相关文件:

app / views / admin / posts / show.html.erb

<h1><%= @post.title %></h1>
<p><%= @post.body %></p><br>
<%= link_to "delete", [:admin, @post], method: :delete %>

controllers / admin / posts_controller.rb

class Admin::PostsController < Admin::ApplicationController
  def index
    @post = Post.all
  end

  def show 
    @post = Post.friendly.find(params[:id])
  end

  def new
    @post = Post.new
  end

  def create
    post_params = params.require(:post).permit(:title, :body, :slug)
    @post = Post.new(post_params)
    @post.save
    redirect_to [:admin, @post]
  end

  def destroy
    @post = Post.friendly.find(params[:id])
    @post.destroy
  end 
end

app / assets / javascripts / admin.js

// 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, 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. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//

//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
//= require jquery
//= require jquery_ujs

应用/资产/样式表/admin.css

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
 * vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope.
 *
 *= require_tree .
 *= require_self
 */

app / views / admin / admin.html.erb

 <!DOCTYPE html>
<html>
  <head>
    <title>JonBlog</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag    'admin', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'admin', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

单击“删除”时控制台输出。

Started GET "/admin/posts/spiral" for 127.0.0.1 at 2018-10-03 23:43:48 +0100
Processing by Admin::PostsController#show as HTML
  Parameters: {"id"=>"spiral"}
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."slug" = ? LIMIT ?  [["slug", "spiral"], ["LIMIT", 1]]
  ↳ /Users/jonathonday/.rvm/gems/ruby-2.3.1/gems/friendly_id-5.2.4/lib/friendly_id/finder_methods.rb:60
  Rendering admin/posts/show.html.erb
  Rendered admin/posts/show.html.erb (1.0ms)
Completed 200 OK in 18ms (Views: 12.2ms | ActiveRecord: 0.2ms)

routes.rb

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  namespace :admin do
    resources :posts
  end

  resources :comments

end

1 个答案:

答案 0 :(得分:0)

请注意,命中路线是一个GET请求,您正在命中show操作。如果您检查页面的HTML,我敢打赌,它与delete方法无关。因此,写的link_to调用一定不能正确传递:method

我有点不习惯了,但是如果您尝试使用类似link_to("Delete", admin_post_path(@post), method: :delete)的东西(即,使用字符串url代替域/资源说明符的数组)会发生什么?