行动'破坏'不能正常运作

时间:2017-10-29 16:38:08

标签: jquery ruby-on-rails ruby destroy

这是我(再次),但现在我遇到的问题是我创建了一个销毁操作,当我想删除帖子并点击名为“删除”的链接时,浏览器显示“显示”页面就像我所做的“阅读帖子”链接与“删除”链接相同。当我点击“删除”时我查看了终端,它显示为GET方法而不是DELETE方法。我已将'link_to'更改为'button_to'并且它可以正常工作,但它不会显示该消息。我需要帮助才能继续学习我正在学习的教程,所以请帮助我:)

我正在使用Rails版本5.1.4,我已经尝试了本网站上的所有内容,但没有任何作用(比如安装jquery gem等等)。

所有代码如下:

帖子控制器:

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all.order("created_at DESC")
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    if @post.save
        redirect_to @post
    else
        render 'new'
    end
  end

  def show
  end

  def edit
  end

  def update
    if @post.update(post_params)
      redirect_to @post
    else
      render 'edit'
    end
  end

  def destroy
    @post.destroy
    redirect_to root_path
  end

  private

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

    def post_params
        params.require(:post).permit(:title, :body)
    end
end

索引页:

<% @posts.each do |post| %>
    <h2><%= post.title %></h2>
    <div>
        <%= truncate(post.body, :lenght => 35) %>
    </div>
    <br>
    <%= link_to "Read Post", post %> 
    <%= link_to "Delete", post, method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>

新:

<h1>New Post</h1>

<%= render 'form' %>

编辑:

<h1>Edit Post</h1>

<%= render 'form' %>

显示:

<h1><%= @post.title %></h1>

<div>
    <%= @post.body %>
</div>
<br>
<%= link_to "Edit Post", edit_post_path(@post) %>

_form:

<%= form_for @post do |f| %>
    <%= f.label :title %> <br>
    <%= f.text_field :title %><br>
    <br>
    <%= f.label :body %><br>
    <%= f.text_area :body %><br>
    <br>
    <%= f.submit %>
<% end %>

路线:

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

终端的Rake Routes命令:

 Prefix Verb   URI Pattern               Controller#Action
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy
     root GET    /                         posts#index

点击“删除”链接时终端输出:

Started GET "/posts/1" for 127.0.0.1 at 2017-10-29 14:33:17 -0200
Processing by PostsController#show as HTML
  Parameters: {"id"=>"1"}
  Post Load (1.0ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT
 ?  [["id", 1], ["LIMIT", 1]]
  Rendering posts/show.html.erb within layouts/application
  Rendered posts/show.html.erb within layouts/application (2.0ms)
Completed 200 OK in 239ms (Views: 166.0ms | ActiveRecord: 2.0ms)

请帮助我!!

2 个答案:

答案 0 :(得分:0)

这可能是因为您遗漏了jquery文件中包含的jquery_ujsapplication.js个库。

app / views / layouts / application.html.erb 中的

添加:

<%= javascript_include_tag 'application' %>

app / assets / javascripts / application.js

//# This file is compiled into one js file
//= require jquery
//= require jquery_ujs

答案 1 :(得分:0)

首先确保已加载lib。我假设它是application.js文件。您还可以检查chrome dev工具中的网络选项卡,以查看文件是否已加载。

//= require jquery
//= require jquery_ujs

我不确定,虽然可能不太可能,但你可以尝试改为:

<% @posts.each do |post| %>
    <h2><%= post.title %></h2>
    <div>
        <%= truncate(post.body, :lenght => 35) %>
    </div>
    <br>
    <%= link_to "Read Post", post %> 
    <%= link_to "Delete", post_path(post), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>

我唯一改变的是link_to 'Delete', post_path(path)...