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.
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?
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.
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 %>
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...