No route matches [POST] "/coachings/4" when trying to use Destroy - ruby-on-rails

coachings GET /coachings(.:format) {:action=>"index", :controller=>"coachings"}
POST /coachings(.:format) {:action=>"create", :controller=>"coachings"}
new_coaching GET /coachings/new(.:format) {:action=>"new", :controller=>"coachings"}
edit_coaching GET /coachings/:id/edit(.:format) {:action=>"edit", :controller=>"coachings"}
coaching GET /coachings/:id(.:format) {:action=>"show", :controller=>"coachings"}
PUT /coachings/:id(.:format) {:action=>"update", :controller=>"coachings"}
DELETE /coachings/:id(.:format) {:action=>"destroy", :controller=>"coachings"}
my routes are correct, here is my view index
<%= link_to 'Destroy', coaching, :confirm 'Are you sure?', :method => :destroy %>
here is my controller
def destroy
#coaching = Coaching.find(params[:id])
#coaching.destroy
respond_to do |format|
format.html { redirect_to coachings_path }
format.json { head :ok }
end
end
any ideas why i get this error? i'm new to RoR this is my first projects i've done by myself.

Use :delete method
<%= link_to 'Destroy', coaching, :confirm => 'Are you sure?', :method => :delete %>

In your link_to you are using a method of destroy which isn't a valid HTTP verb so Rails is probably defaulting to POST. You'll need to use DELETE instead:
<%= link_to 'Destroy', coaching, confirm: 'Are you sure?', method: :delete %>

Related

Rails - delete method not working

I am trying to delete an entry as specified in the docs but I keep getting en error stating NoMethodError in Tasks#show and the entry is not deleted.
index.html.erb:
<%= link_to 'Delete', destroy_task_path(task['id']), data: { confirm: 'Are you sure?' } %>
route.rb:
delete '/tasks/:id', to: 'tasks#destroy', as: 'destroy_task'
resources :tasks
root 'home#index'
tasks_controller.rb
def destroy
uri = URI.parse("http://localhost/tasks/public/api/tasks/"+params[:id])
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Delete.new(uri.path)
redirect_to :tasks, notice: 'Task was successfully destroyed.'
end
What am I doing wrong here?! and why does it get redirected to show?!
You missed in your link_tocall the method: :delete, otherwise you do a GET call.

Delete method in ROR route error when using remote true

I'm trying to delete post with the remote: true command in rails. Everything is working fine when i'm not using ajax. But when i use the remote true command i get an routing error.
view:
<% #posts.each do |post| %>
<%= post.title %>
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
<% end %>
controller:
def index
#posts = Post.all
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to :back
end
routes:
resources :posts
This is the error i get in the log.
Started DELETE "/posts" for 127.0.0.1 at 2014-10-11 12:46:33 +0200
ActionController::RoutingError (No route matches [DELETE] "/posts"):
Thanks in advance.
Update. I get this when i write rake routes.
DELETE /posts/:id(.:format) posts#destroy
To get this to working.
Change:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
redirect_to :back
end
To this:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.js { head :no_content }
end
end
and:
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
to:
<%= link_to 'delete', post, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
Incase anyone is having any problems with this, I managed to solve it by adding :data => { :type => :json } to the link.
For example:
<%= link_to "Delete this post", #post, :method => :delete, :remote => true, :data => { :type => :json } %>
I imagine UJS is expecting HTML, however JSON is returned so it fails and posts again (I noticed two requests).
Hope this helps!

The link for deleting item returns undefined method

This is the relevant part of the routes:
resources :photos, :path => '', :only => [:show, :new, :create, :destroy, :edit] do
...
end
And I am added into a view a link for deleting a photo:
<%= button_to 'Delete', #photo, :method => :delete, :confirm => 'Are you sure?' %>
But this view returns the error:
undefined method `photo_path' for #<#<Class:0x007faef8172060>:0x007faef87a7908>
The destroy method is the method generated by scaffold. What do I have wrong?
EDIT: rake routes output:
root / photos#index
search GET /search(.:format) photos#search
voteup_user_photo GET /:user_id/:id/voteup(.:format) photos#voteup
votedown_user_photo GET /:user_id/:id/votedown(.:format) photos#votedown
user_photos POST /:user_id(.:format) photos#create
new_user_photo GET /:user_id/new(.:format) photos#new
edit_user_photo GET /:user_id/:id/edit(.:format) photos#edit
user_photo GET /:user_id/:id(.:format) photos#show
DELETE /:user_id/:id(.:format) photos#destroy
It appears that you have a nested route (path needs :user_id and :id). Try passing the path parameters explicitly:
button_to 'Delete', { :user_id => #user.id, :id => #photo.id }, :method => :delete, :confirm => 'Are you sure?'
If that doesn't work, you may need to include the :action in the options hash as well.

Cannot Edit or View Post with Comments App

I have built a simple Rails app that has posts with one/many comments.
I want to create a simple post view that allows me to view the post and associated comments. I want each comment to have links to - view, edit, delete.
However whenever I try amending the code below I get routing errors. Help?
routes.rb
resources :posts do
resources :comments
end
rake routes
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
comments_controller.rb
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.html
format.json { render :json => #post }
end
end
def edit
#comment = Comment.find(params[:id])
end
comments\show.html.erb
<p>
<b>Commenter:</b>
<%= #comment.user_id %>
</p>
<p>
<b>Comment:</b>
<%= #comment.text %>
</p>
<%= link_to 'View Comment', comment_path(?) %> |
<%= link_to 'Edit Comment', edit_comment_path(?) %> |
<%= link_to 'Delete Comment', [#post, comment],
:confirm => 'Are you sure?',
:method => :delete %></p>
Are you seeing:
Routing Error
No route matches {:action=>"show", :controller=>"comments"}
Try running rake routes for more information on available routes.
I duplicated your project with the code you provided and only received that routing error because there wasn't an id being passed to the route helper methods. Because these are restful routes, the format for View Comment should be /comments/:id(.:format).
I was able to resolve this error by passing an id or comment object to the comment_path and edit_comment_path helper methods like so:
<%= link_to 'View Comment', comment_path(2) %> |
<%= link_to 'Edit Comment', edit_comment_path(3) %> |
<%= link_to 'Delete Comment', [#post, comment],
:confirm => 'Are you sure?',
:method => :delete %></p>
Obviously you would want to populate them with the correct ids or comment objects rather than just some random id.
Hope this helps.
Cheers!

Namespace routing rails nightmare

I am in the process of still learning Rails 3 but routes are driving me crazy. I am trying to use a namespace to separate an administration section of the site. Problem is that some things in the namespace simply don't work and also route to the wrong place. For example using rails generated routes by specifying a resource the view points to the wrong route when passed an object so the edit form won't work.
Links with link_to don't work either even when the route does exist it says it doesn't. Firstly here is the namespaced routes output from rake routes.
namespace :admin do
resources :users
end
admin_users GET /admin/users(.:format) {:action=>"index", :controller=>"admin/users"}
POST /admin/users(.:format) {:action=>"create", :controller=>"admin/users"}
new_admin_user GET /admin/users/new(.:format) {:action=>"new", :controller=>"admin/users"}
edit_admin_user GET /admin/users/:id/edit(.:format) {:action=>"edit", :controller=>"admin/users"}
admin_user PUT /admin/users/:id(.:format) {:action=>"update", :controller=>"admin/users"}
DELETE /admin/users/:id(.:format) {:action=>"destroy", :controller=>"admin/users"}
Controller:
class Admin::UsersController < ApplicationController
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def edit
#user = User.find(params[:id])
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to(#user, :notice => 'User was successfully created.')
else
render :action => "new"
end
end
def update
#user = User.find(params[:id])
if #user.update_attributes(params[:user])
redirect_to(admin_users_path, :notice => 'User was successfully updated.')
else
render :action => "edit"
end
end
def destroy
#user = User.find(params[:id])
#user.destroy
redirect_to(admin_users_path)
end
end
Example view: index.html.erb listing all users
<h1>Listing users</h1>
<table>
<% for user in #users %>
<tr>
<td><%= user.id %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', #user %></td>
<td><%= link_to 'Edit', edit_admin_user_path(user) %></td>
<td><%= link_to 'Destroy', admin_user_path, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_admin_user_path %>
Using the edit view is also having a problem. The edit form should point to the update route but does not. Instead it points to the edit route (basically itself) when only being passed a User object. From what I have been reading using an object in forms is the recommended way but this cant be a good thing if it does not work.
I get this error on listing all users page.
No route matches {:action=>"update", :controller=>"admin/users"}
Extracted source (around line #17):
17: <td><%= link_to 'Destroy', admin_user_path, :confirm => 'Are you sure?', :method => :delete %></td>
I am so trying to persevere but this is driving me loopy. FYI: Yes I know there are authentication frameworks out there but I am trying to make one from scratch. This is a learning experience and as such just using gems and plugins willy nilly is not the way to go in my opinion.
Thank you
Onyth
You're missing the id in the delete link
Try with
<td><%= link_to 'Destroy', admin_user_path(user), :confirm => 'Are you sure?', :method => :delete %></td>
(changed admin_user_path to admin_user_path(user) as the link)
If you do a rake routes you would see something like this:
admin_users GET /admin/users(.:format) {:controller=>"admin/users", :action=>"index"}
POST /admin/users(.:format) {:controller=>"admin/users", :action=>"create"}
new_admin_user GET /admin/users/new(.:format) {:controller=>"admin/users", :action=>"new"}
edit_admin_user GET /admin/users/:id/edit(.:format) {:controller=>"admin/users", :action=>"edit"}
admin_user GET /admin/users/:id(.:format) {:controller=>"admin/users", :action=>"show"}
PUT /admin/users/:id(.:format) {:controller=>"admin/users", :action=>"update"}
DELETE /admin/users/:id(.:format) {:controller=>"admin/users", :action=>"destroy"}
admin_pages GET /admin/pages(.:format) {:controller=>"admin/pages", :action=>"index"}
POST /admin/pages(.:format) {:controller=>"admin/pages", :action=>"create"}
new_admin_page GET /admin/pages/new(.:format) {:controller=>"admin/pages", :action=>"new"}
edit_admin_page GET /admin/pages/:id/edit(.:format) {:controller=>"admin/pages", :action=>"edit"}
admin_page GET /admin/pages/:id(.:format) {:controller=>"admin/pages", :action=>"show"}
PUT /admin/pages/:id(.:format) {:controller=>"admin/pages", :action=>"update"}
DELETE /admin/pages/:id(.:format) {:controller=>"admin/pages", :action=>"destroy"}
So you can derive
admin_user_path
which would be the same as
user_path
Then you would pass the #user in admin_user_path like so:
admin_user_path(#user)
The :method should then call the destroy method instead of going to the show method automatically! :)
To get the form_for working, I found the following resource: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Under the form_for section, they explain namespace routing with form_for as:
For namespaced routes, like admin_post_url:
<%= form_for([:admin, #post]) do |f| %>
...
<% end %>
For information regarding rake routes check out: http://guides.rubyonrails.org/command_line.html#rake-is-ruby-make then under section 2.4.9 Miscellaneous Tasks they explain rake --tasks shows you various rake commands that you can use and rake routes shows you route paths available.
Hope this helps!

Resources