簡體   English   中英

無法獲得“刪除”鏈接以在Ruby on Rails中的管理面板上工作

[英]Unable to get “delete” link to work on an admin panel in Ruby on Rails

我是Ruby On Rails的新手,當前正在為在應用程序中手動構建的管理面板創建標准的CRUD操作(不使用管理工具)。

但是,當我在show.html.erb頁面上調用刪除鏈接時,什么也沒有發生(錯誤屏幕或瀏覽器中沒有任何其他反饋)。 Javascript已完全啟用,我相信我已安裝了正確的gem。

有人可以讓我知道解決此問題的最佳方法嗎? 我將在下面鏈接所有相關文件:

應用程序/視圖/管理/職位/ show.html.erb

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

控制器/管理/ 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

應用程序/資產/ JavaScript的/ 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
 */

應用程序/視圖/管理/ 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

請注意,命中路線是GET請求,您正在命中show動作。 如果您檢查頁面的HTML,我敢打賭,它與delete方法無關。 因此,寫的link_to調用一定不能正確傳遞:method

我有點link_to("Delete", admin_post_path(@post), method: :delete) ,但是如果您嘗試使用諸如link_to("Delete", admin_post_path(@post), method: :delete) (即使用字符串url而不是用於域/資源說明符的數組)會發生什么? )?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM