ruby-on-rails Ruby on Rails销毁不工作

kxxlusnw  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(247)

我不能让销毁方法工作。当我点击删除,什么也没发生。没有确认弹出,帖子不被删除。控制器中的方法:

def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to posts_path, :notice => "Your post has been deleted successfully."
end

查看:

<%= link_to "Delete post", @post, data: { confirm: "Are you sure?" }, :method => :delete %>

呈现的HTML:

<a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/8">Delete post</a>

application.html.erb文件是错误创建的:

<!DOCTYPE html>
<html>
<head>
  <title>Myrubyblog</title>
  <%= stylesheet_link_tag 'application', media: 'all', 'dataturbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<% flash.each do |key, value| %>
<p><%= value %></p>
<% end %>
<%= yield %>

</body>
</html>

编辑和其他一切工作正常。有什么想法,使这个工作?谢谢
解决了!问题是在application.html.erb文件javascript_include_tag中。当我开始这个项目时,我得到了“Rails ExecJS::ProgramError in Pages#home”,所以我把javascript_include_tag“application”改为“default”(正如人们在这个线程stackoverflow. com/questions/28421547/...中所说的那样)。现在我把它更新为“application”,它就像它应该的那样工作了。谢谢,Kevin!

erhoui1w

erhoui1w1#

试试这个

<%= link_to 'Delete post', @post, method: :delete, data: { confirm: 'Are you sure?' } %>

相关问题