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

On 5 Dec, 17:49, Rick DeNatale <[email protected]> wrote:
> On Sat, Dec 5, 2009 at 10:40 AM, RubyonRails_newbie
>
>
>
>
>
> <[email protected]> wrote:
> > 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])
> >   �[email protected]
>
> >    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?
>
> 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 %>
> --
> Rick DeNatale
>
> Blog:http://talklikeaduck.denhaven2.com/
> Twitter:http://twitter.com/RickDeNatale
> WWR:http://www.workingwithrails.com/person/9021-rick-denatale
> LinkedIn:http://www.linkedin.com/in/rickdenatale

--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to