我正在通过入门教程(创建一个博客)和link_to Destroy无法正常运行.在终端中,它总是将其解释为#SHOW.
在阅读类似问题时,我了解到删除必须转换为POST以供浏览器解释.这似乎没有发生.
我也在Lynda.com Rails课程中使用Destroy遇到了同样的问题,所以它让我相信它是我开发环境中的东西.我在MacBook Pro Lion上使用Rails 4,Ruby 2.00p274,MySQL,WEBrick作为HTTP服务器.
在选择Destroy时的终端会话中:
Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600
Processing by PostsController#show as HTML
  Parameters: {"id"=>"4"}
  Post Load (0.6ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", "4"]]
  Rendered posts/show.html.erb within layouts/application (0.4ms)
Completed 200 OK in 13ms (Views: 8.6ms | ActiveRecord: 0.6ms)
在ports-controller.rb中:
def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to action: :index
end
在index.html.erb中:
<% @posts.each do |post| %>
  <tr>
    <td><%= post.title %></td>
     <td><%= post.text %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy',  { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
 <% end %>
在routes.rb中
Blog::Application.routes.draw do
   resources :posts do
     resources :comments
   end
  root to: 'welcome#index'
end
haw*_*awk 52
试试这个
<%= link_to 'Destroy',  post,  method: :delete, data: { confirm: 'Are you sure?' } %>
小智 41
确保gem'jquery-rails'在gemfile中,jquery_ujs包含在app/assets/javascripts/application.js文件中
//= require jquery
//= require jquery_ujs
资料来源:https://github.com/rails/jquery-ujs
小智 7
我通过添加assets/javascripts/application.js解决了这个问题
//= require jquery_ujs
Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600
这就是问题。删除链接正在使用 GET http 动词,即使您method: delete在链接中使用了该动词。
| 归档时间: | 
 | 
| 查看次数: | 56804 次 | 
| 最近记录: |