Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

Rails 4 link_to Destroy not working in Getting Started tutorial

I am working through the Getting Started tutorial (creating a Blog) and the link_to Destroy is not functioning properly. In the terminal it always interprets it as #SHOW.

In reading similar issues I have learned that the Delete must be converted to a POST for the browser to interpret it. This doesn't seem to be happening.

I have run into the same problem using Destroy in the Lynda.com Rails course as well, so it leads me to believe it is something in my development environment. I am using Rails 4, Ruby 2.00p274, MySQL, WEBrick for the HTTP server on a MacBook Pro Lion.

in the terminal session when Destroy is selected:

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)

In the ports-controller.rb:

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

    redirect_to action: :index
end

In the 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 %>

In the routes.rb

Blog::Application.routes.draw do
   resources :posts do
     resources :comments
   end
  root to: 'welcome#index'
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...