deleting model objects

View: New views
5 Messages — Rating Filter:   Alert me  

deleting model objects

by Aryk Grosz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


def remove_req
    @posts = Post.find_all_by_requirement_id(params[:id])

    @posts.each do |post|
      post.destroy
    end

    redirect_to :action => :index
  end

<%= link_to 'Remove', :url => { :action => 'remove_req' },
:id=>post.requirement_id, :method => :delete %></td>

Doesn't work, any tips?

Thanks!
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: deleting model objects

by Aryk Grosz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Justin To wrote:

> def remove_req
>     @posts = Post.find_all_by_requirement_id(params[:id])
>
>     @posts.each do |post|
>       post.destroy
>     end
>
>     redirect_to :action => :index
>   end
>
> <%= link_to 'Remove', :url => { :action => 'remove_req' },
> :id=>post.requirement_id, :method => :delete %></td>
>
> Doesn't work, any tips?
>
> Thanks!


Try this...

Post.destroy params[:id]

If you don't have any logic around the destroy (before_destroy,
:dependent => :destroy, etc.), Post.delete params[:id] would be a little
faster.
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: deleting model objects

by Aryk Grosz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


remove_req.rjs
page.replace_html :main-body, :partial => 'requirement_list'

view:
<%= link_to_remote 'Remove', :url => { :action => 'remove_req' }, :with
=> "'id=#{post.requirement_id}'" %>

control:
  def remove_req
    Post.delete_all "requirement_id = #{params[:id]}"
    respond_to do |format|
          format.html { redirect_to :action => :index }
          format.js { flash[:notice] = "ok" }
      end
  end

It doesn't replace the html =(

Suggestions?

Thanks!
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: deleting model objects

by Aryk Grosz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> It doesn't replace the html =(
>
> Suggestions?
>
> Thanks!

Your Ajax request may be failing. Take a look at your log, or the
Firebug console if you use it.
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: deleting model objects

by Phlip :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Justin To wrote:

> def remove_req
>     @posts = Post.find_all_by_requirement_id(params[:id])
>
>     @posts.each do |post|
>       post.destroy
>     end
>
>     redirect_to :action => :index
>   end
>
> <%= link_to 'Remove', :url => { :action => 'remove_req' },
> :id=>post.requirement_id, :method => :delete %></td>

Put the :id inside the :url. From outside, it might be the HTML id.

> Doesn't work, any tips?

Put the deleter into a class method on Post.

Write a unit test for it.

(Actually, write the test first, then put the deleter in, but that's an advanced
topic...)

Get the test to pass, by detecting Post.find_by_requirement_id() returns nil.

Don't use find_all_by_requirement_id if your params[:id] can only have one.

Then write a "functional" test checking that your action deletes the target record.

And don't use :method => :delete in your link. That links to a magic HTTP verb
DELETE, which you probably are not implementing. Try :method => :post.

--
   Phlip


--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price