How to delete a record inside of an association


#1

I’m a noob having a hard time, and it’s probably really simple.

Here’s the way it’s set up:
Client has many Bins
Bin belongs to Client
In my Client show view I have it render a partial that lists all the
records in Bin associated with Client.

I need to have a destroy link for each of the Bin records. Right now I
have it configured in my Bin partial as:
<%= link_to(“Delete”, { :action => “destroy”, :id => @bin }, :confirm
=> “Are you sure?”, :method => :delete) %>

This just throws back an error saying that there is not a Client with
that id.

I can’t figure out how to tell it to delete the bin not the client
with that id.

Any helpful suggestions? Cheers.


#2

On 20 Mar., 20:57, Nate removed_email_address@domain.invalid wrote:

<%= link_to(“Delete”, { :action => “destroy”, :id => @bin }, :confirm
=> “Are you sure?”, :method => :delete) %>

This just throws back an error saying that there is not a Client with
that id.

I can’t figure out how to tell it to delete the bin not the client
with that id.

Any helpful suggestions? Cheers.

I guess it’s because, that when you’re rendering partials you’ll not
be given an instance variable, but a local variable. You can try to
see if this is enough to fix the problem:

<%= link_to(“Delete”, { :action => “destroy”, :id =>
bin.id }, :confirm
=> “Are you sure?”, :method => :delete) %>

I simply removed the @ to reference a local variable instead. (And I
call the #id method on the Bin, but that’s optional.)


Cheers,
David K.
http://twitter.com/rubyguy


#3

On Mar 20, 10:39 pm, “removed_email_address@domain.invalid” removed_email_address@domain.invalid wrote:

records in Bin associated with Client.
with that id.

Any helpful suggestions? Cheers.

I guess it’s because, that when you’re rendering partials you’ll not
be given an instance variable, but a local variable. You can try to
see if this is enough to fix the problem:

No. The problem is the current controller is the client controller,
so :action => destroy routes you to /clients/destroy, which
unsurprisingly destroys clients not bins. If you’re feeling old school
you would add :controller => ‘bins’ to your hash, but a more modern
way to do this would be

link_to ‘Delete’, bin_path(@bin), :method => :delete

Fred