【发布时间】:2017-11-04 11:36:09
【问题描述】:
无法删除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
应用程序.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 .
【问题讨论】:
-
您已经重启了服务器,但它仍然无法正常工作?
-
您的日志文件中的请求是什么样的?你的
app/assets/javascripts/application.js怎么样?您的布局文件中是否包含该 JavaScript 文件? -
@spickermann 我已经在上面添加了 application.js 信息,我取消了那些
require的注释,因为之前给了我类似Object doesn't support this property or method的错误 -
您可以通过这个简单的语句检查错误 -
puts @post.errors.messages并查看终端的输出,只需将其写在您的@post.destroy下方,在您的控制器的destroy操作中。
标签: ruby-on-rails ruby