my routes.rb
root 'pages#home'
get 'about', to: 'pages#about'
resources :articles
match ':controller(/:action(/:id))', :via => [:get, :post]
my articles_controller.rb
def delete
@article = Article.find(params[:id])
end
def destroy
article = Article.find(params[:id])
article.destroy
flash[:notice] = "Article destroyed successfully."
redirect_to(:action => 'index')
end
我的delete.html.rb
<h2>Delete Article</h2>
<div class="subjects delete">
<h2>Delete Subject</h2>
<%= form_for(:article, :url => {:action => 'destroy', :id => @article.id}) do |f| %>
<p>Are you sure you want to permanently delete this subject?</p>
<p><%= @article.title %></p>
<div class="form-buttons">
<%= submit_tag("Delete Article") %>
</div>
<% end %>
</div>
现在问题是当我点击编辑文章重定向到
http://localhost:3000/articles/7/edit
显示重定向到localhost:3000/articles/7
删除重定向到localhost:3000/articles/delete/8
的位置
当点击删除按钮时,它显示The action '8' could not be found for ArticlesController
为什么我不会被重定向到localhost:3000/articles/8/delete
我将routes.rb更改为
match ':controller(/:id(/:action))', :via => [:get, :post]
在此删除中有效但编辑不是。
答案 0 :(得分:0)
您最好使用路由的resources
指令:
#config/routes.rb
resources :articles #-> url.com/articles/:id/destroy
resources :pages, only: [], path: "" do
get :about, on: :collection #-> url.com/about
end
root 'pages#home'
以上将提供正确的路线。
您的问题是您提交表单以尝试更改该值。这不起作用;你需要:
#app/views/articles/show.html.erb
<%= button_to "Delete", article, method: :delete, data: { confirm: "Are you sure?" } %>
这将调用您的posts#destroy
控制器&amp;解雇你的代码(看起来没问题)。