RJS method outputting the original javascript
I have a page where I am dynamically adding items from a series of dedicated boxes. I am using RJS for this and it works great. These elements in the div are now a series followed by Delete buttons if the user wants to delete the text box. Here is where I hit the wall.
Here's the code that triggers the Delete button. This works well:
<%= link_to image_tag("/images/button_delete.gif", :alt=>"Delete"), :controller=>"report", :action=>"remove", :id=>@sentence.id %>
In my report controller, I have this very simple method called by the above code:
def delete @sentence_id = params [: id] end
Again, I think I am working. Now when I activate this action by clicking the button, exit to the RJS file, remove.rjs:
page.remove 'sentence_'+@sentence_id
And here's what I get in my browser, not a happily deleted element!
try
{
Element.remove("sentence_63");
}
catch (e)
{
alert('RJS error:\n\n' + e.toString());
alert('Element.remove(\"sentence_63\");');
throw e;
}
I understand that this happens when there is no page in the current context. Online documentation on this is a bit thin.
Any help is appreciated! Hurray, Aaron.
a source to share
Since your function remove
doesn't actually remove the entry, if you just want to remove the HTML element from the page, you can use link_to_function
the Prototype remove()
method on the elements. In addition, if you have the latest version of Rails (e.g. 2.3.2), you can use dom_id
helper to automatically generate the sentance_id
ID attribute
<%= link_to_function(image_tag("button_delete.gif", :alt=>"Delete"), "$('#{dom_id(@sentence}').remove();" %>
This approach can help keep the number of methods in your controller (unless you intend to do something else in the controller)
a source to share
The "Delete" link is configured as a regular link, i.e.
<a href="/report/remove" id="sentence_63">
<img src="/images/button_delete.gif" alt="Delete" />
</a>
which starts a normal HTTP request. Since you intend to initiate an AJAX request try the PJ Davis recommendation and uselink_to_remote
a source to share