Forum: Ruby on Rails trouble creating a link or button to delete (more than basic delete from index)

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.
Chris W. (Guest)
on 2009-02-21 04:53
(Received via mailing list)
Hi all,

I'm using Rails 2.2.2.

I'm trying to create a hyperlink that will delete an image when it's
clicked.  However I need to
1. pass along a bit more info (init_entity_id)
2. call this from a different part of the app (the entity page has
images on it, and clicking the delete link next to the image will
delete that image)
3. specify the class of the link

I can get it working if I don't specify the class and if I skip the
init_entity_id bit:
    <%= link_to '[X]', entity_image, :controller  => entity_images',
:method => :delete, :confirm => 'Are you sure?' %>

I can probably work around the CSS issue by putting that in a span or
something. I'd been planning on using the init_entity_id to return to
the entity after the image is deleted. If there were a handy way to
get back there with out passing along that entity's ID I could get
this working without the below mess....

I've tried
    <%= link_to '[X]', {:controller  => 'entity_images', :action =>
'destroy', :id => entity_image, :init_entity_id => @entity}, :confirm
=> 'Are you sure?', :title => 'delete this', :class =>
'link_to_delete' %>
which creates
    <a href="/entity_images/1?init_entity_id=1" class="link_to_delete"
onclick="return confirm('Are you sure?');" title="delete this">[X]</a>
which shows the image instead of deleting it.

I've tried
    <%= link_to '[X]', {:controller  => 'entity_images', :method =>
:delete, :id => entity_image, :init_entity_id => @entity}, :confirm =>
'Are you sure?', :title => 'delete this', :class => 'link_to_delete'
%>
which creates
    <a href="/entity_images/index/1?init_entity_id=1&amp;method=delete"
class="link_to_delete" onclick="return confirm('Are you sure?');"
title="delete this">[X]</a>
which lists all the images instead of deleting it.

I've tried
    <%= link_to '[X]', :controller  => 'entity_images', :action =>
'destroy', :id => entity_image, :init_entity_id => @entity, :confirm
=> 'Are you sure?', :title => 'delete this', :class =>
'link_to_delete' %>
which creates
    <a
href="/entity_images/1?class=link_to_delete&amp;confirm=Are+you+sure%3F&amp;init_entity_id=1&amp;title=delete+this">[X]</a>
which shows the image instead of deleting it.

I've tried
    <%= link_to '[X]', {:controller  => 'entity_images', :id =>
entity_image, :init_entity_id => @entity}, :method => :delete,
:confirm => 'Are you sure?', :title => 'delete this', :class =>
'link_to_delete' %>
which creates
    <a href="/entity_images/index/1?init_entity_id=1"
class="link_to_delete" onclick="if (confirm('Are you sure?')) { var f
= document.createElement('form'); f.style.display = 'none';
this.parentNode.appendChild(f); f.method = 'POST'; f.action =
this.href;var m = document.createElement('input');
m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
m.setAttribute('value', 'delete'); f.appendChild(m);var s =
document.createElement('input'); s.setAttribute('type', 'hidden');
s.setAttribute('name', 'authenticity_token'); s.setAttribute('value',
'ad000f9b86a2ecabed45f0b1a103681db863d724');
f.appendChild(s);f.submit(); };return false;" title="delete
this">[X]</a>
which lists all the images instead of deleting it.

Most other variations on link_to I've tried either produce outright
errors in generating the link, or similarly just show the image or
list all images.

I've also tried
    <%= button_to '[X]', {:controller  => 'trait_value_images',
:action => "destroy", :id => trait_value_image, :init_trait_value_id
=> @trait_value}, :method => :delete, :confirm => 'Are you sure?',
:title => 'delete this' %>
which creates
    <form method="post"
action="/trait_value_images/7?init_trait_value_id=1"
class="button-to"><div><input name="_method" type="hidden"
value="delete" /><input onclick="return confirm('Are you sure?');"
title="delete this" type="submit" value="[X]" /><input
name="authenticity_token" type="hidden"
value="ad000f9b86a2ecabed45f0b1a103681db863d724" /></div></form>
which does do the delete, but the init_trait_value_id is not sent.

Basically, I think I need a way to specify that the auto-generated
mini-form include something like <input type="hidden"
name="init_trait_value_id" value="1" /> for either the link
(preferred) or the button.

Any advice on how to get this working?

Thanks
   -Chris W
Chris (Guest)
on 2009-02-21 05:03
(Received via mailing list)
It figures I'd finally find the solution about 2 minutes after giving
up and posting to the list :/

> If there were a handy way to
> get back there with out passing along that entity's ID I could get
> this working without the below mess....

What I needed was apparently just

    redirect_to request.referer
This topic is locked and can not be replied to.