When I click on the ‘delete’ link (to delete the project), I wish to show a new page for the user to confirm the deletion (‘are you sure’ page). But when I click on the link, error No route matches [GET] “/assets” appears, I don’t know why. Can anyone help me?
This is my projects/index.html.erb
1 <% @projects.each do |p| %>
2 <%= link_to p.name, project_path(p) %>
3 <%= link_to "edit", edit_project_path(p) %>
4 <%= link_to "delete",{ :action => 'delete', :controller => 'projects', :id => p.id } %>
5 <% end %>
This is my controllers/projects_controller.rb
38 def delete
39 @project = Project.find(params[:id])
40 render 'delete'
41 end
42
43 def destroy
44 project = Project.find(params[:id])
45 project.destroy
46 flash[:notice] = "Page succesfully removed"
47 redirect_to(projects_path)
48 end
This is my views/projects/delete.html.erb
1 <%= form_for(:project, :url => {:action => 'destroy', :id => @project.id }) do |f| %>
2 <p>Are you sure you want to remove "<%= @project.name %>"</p>
3 <%= submit_tag ("Yes, I am sure")
4 <% end %>
EDIT:My Url looks like this >>> http://localhost:3000/assets?action=delete&controller=projects&id=1
EDIT2: the solution is going to routes.rb and adding this line
match ':controller(/:action(/:id))(.:format)'
Now the delete page appears. However, when I click the delete button Rails returns an error >> Unknown action The action ‘6’ could not be found for ProjectsController
The problem is this line:
You’re not telling it to use the HTTP method DELETE when making this
You could also write this much, much shorter like this:
Rails will know precisely what URL to build, so you don’t need to tell it what to do.
Bonus
You could also do this with the initial
link_to:The array as the second argument here will compile the route using the polymorphic route builder in Rails.