Struggling to delete posts... Can you help?

Hello there,

I am looking to be able to delete posts from within my account page.

I have a destroy action within the posts controller, which looks like this:

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

    respond_to do |format|       format.html { redirect_to :controller => "profile"}       format.xml { head :ok }     end   end

And I also have the button which is clicked to delete a post.

  <% form_tag(:controller => "posts", :action => "destroy", :confirm => 'Are you sure?') do %>     <%= submit_tag("Delete") %>

However - when i click 'delete' I get the following error:

ActiveRecord::RecordNotFound in PostsController#destroy

Couldn't find Post without an ID

I changed '@post = Post.find(params[:user_id])' to only have :id as a param, but that returned with the same issue.

Can anyone help?

Many Thanks

I don't see where you are actually passing in the id!

We do something like this:

<%= link_to image_tag("cross.png"), post, :confirm => 'Are you sure?', :method => :delete, :title => "Delete this post" %>

That gives us an icon to click on and will delete the post by calling PostsController#destroy

which looks like:

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

    respond_to do |format|       format.html { redirect_to(posts_url) }       format.xml { head :ok }     end   end

It doesnt looks like the form has the id parameter for the Post.

Kristian

Sorry forgot to say that in the view that line is inside an iterator:

<% for post in @posts %> <%= link_to image_tag("cross.png"), post, :confirm => 'Are you sure?', :method => :delete, :title => "Delete this post" %> <% end %>

where @posts is set by: @posts = Post.all in the PostsController#index method.

Cool - thank you for that. It makes sense to me now.

What I now have is this:

       <%= image_tag ("delete.png") %>   <%= link_to "Delete post?",       { :controller => "posts", :action => "delete",         :id => @posts.id },         :confirm => "Really delete?" %> |

This successfully deletes the posts, one at a time until they have all gone. However, once the last post of that user has been deleted, I get an error:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

I know this is because it cannot find any posts for that post.id, but is there a good if else statement i can add to it to render the profile page once all have been erased?

I've looked over it, but am a bit lost..

THanks again

Without seeing your view, I'll guess that you're using "for foo in @foo do" ...if you use "@foo.each do |foo|" it should work, since .each doesn't return an error on nil like 'for' does.

-eric

my view is:

       <%= image_tag ("delete.png") %>   <%= link_to "Delete post?",       { :controller => "posts", :action => "delete",         :id => @posts.id },         :confirm => "Really delete?" %> |

i've tried to add <% if Posts.exists? %> before this so it would only display the delete icon if there are posts to delete...

however, this also doesn't work..

full code:

  <% if Posts.exists? %>

       <%= image_tag ("delete.png") %>   <%= link_to "Delete post?",       { :controller => "posts", :action => "delete",         :id => @posts.id },         :confirm => "Really delete?" %> |

<% else %>

  <%= image_tag ("another_image.png") %>

  <% end %>

I see two problems here.

1) In the controller you are using params[:user_id] when it should be params[:id]

2) Your form isn't telling the controller what to delete.     instead of <% form_tag(:controller => "posts", :action => "destroy", :confirm => 'Are you sure?') do %>         <%= submit_tag("Delete") %> <% end %>

maybe

<% form_for @post, :url => {:action => 'destroy'} do %>       <%= submit_tag("Delete", :confirm => 'Are you sure?' %> <% end %>

Ive since updated my controller to :id

THe posts delete fine, until there are no posts left for that id to delete, then I get the error: Showing app/views/user/index.html.erb where line #40 raised:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

I need an if posts.exists? or someting similar to allow the page to render regardless of the number of posts...

@post.destroy if @post

Nope - sorry - this doesn't work either.

I will have to do some more digging about I think.

THanks anyway...