deleting model objects

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!

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.

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!

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.

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.