Forum: Ruby on Rails How to delete a record inside of an association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
23992d198a1bc2190c539a32fc960069?d=identicon&s=25 Nate (Guest)
on 2009-03-20 21:34
(Received via mailing list)
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.
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 rubyguy@ymail.com (Guest)
on 2009-03-20 23:40
(Received via mailing list)
On 20 Mar., 20:57, Nate <CrossRoadStud...@gmail.com> 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 Knorr
http://twitter.com/rubyguy
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-21 00:12
(Received via mailing list)
On Mar 20, 10:39 pm, "ruby...@ymail.com" <ruby...@ymail.com> 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
This topic is locked and can not be replied to.