无法删除rails中的帖子

时间:2017-11-04 11:36:10

标签: ruby-on-rails ruby

无法删除rails中的帖子,每当我点击删除按钮时它什么都不做,它甚至不显示确认消息也没有任何错误。我该如何解决这个问题?我应该提供除下述细节以外的更多细节吗?

posts_controller.rb

class PostsController < ApplicationController

 def index
    @posts =Post.all

 end

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


 def new
   @post= Post.new
 end

 def create
    @post = Post.new(post_params)
    @post.save
    redirect_to @post
 end

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

 def update
    @post= Post.find(params[:id])
    if(@post.update(post_params))
        redirect_to @post
    else
        render'edit'
    end

 end

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

    redirect_to posts_path
 end



 private def post_params
    params.require(:post).permit(:Name, :Country, :Details)
 end

 end

show.html.erb

<%= link_to "Edit", edit_post_path(@post), :class => 'btn btn-default' %>
<%= link_to "Delete", post_path(@post), 
                    method: :delete,
                    data: {confirm:'Are you Sure?'},
                    :class => 'btn btn-danger' %>

edit.html.erb

<h1>Edit Vacation</h1>

<%= form_for :post, url: post_path(@post), method: :patch  do |f| %>

<p>
<%= f.label :Name %><br>
<%= f.text_field( :Name, {:class=> 'form-control'} )%>
</p>

<p>
<%=f.label :Country %><br>
<%= f.text_field( :Country, {:class=> 'form-control'}) %>
</p>

<p>
<%=f.label :Details %><br>
<%= f.text_area( :Details, {:class=> 'form-control'}) %>
</p>

 <p>
 <%= f.submit({:class => 'btn btn-primary'}) %>
 </p>
 <% end %>

的routes.rb

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

root 'posts#index'

resources :posts

end

的application.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 turbolinks
= require_tree .

2 个答案:

答案 0 :(得分:-1)

在您的控制器中:

def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to postss_url, notice: 'post was successfully destroyed.' }
      format.json { head :no_content }
    end
end

答案 1 :(得分:-2)

我通过在application.js中回复那些require然后在运行gem 'coffee-script-source', '1.8.0'之后在gem文件中添加bundle update coffee-script-source来解决这个问题并重启服务器因此我能够删除帖子