Tuesday, May 3, 2011

javascript in a rails :url parameter

I want to create a drop_receiving_element where the :url contains javascript. Currently I just created a helper function and hard-coded the element in like so:

def create_classification_droppable(droppable_id, classification)
  "<script type='text/javascript'>
    //<![CDATA[
      Droppables.add('#{droppable_id}', {accept:'lead', onDrop:function(element){new Ajax.Request('/leads/' + (element.id.split('_').last()) + '.js', {asynchronous:true, evalScripts:true, method:'put', parameters:'lead[classification]=#{classification}&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')})}})
    //]]>
  </script>"
end

That's pretty hackish and ugly, though. Ideally I'd like to do something like:

drop_receiving_element('some_class',                 
  :accept => 'some_other_class',
  :url    => formatted_whatever_path( SOMETHING_BASED_ON_WHATEVER_IS_BEING_DROPPED ) )

Or

formatted_whatever_path(:id => "some_javascript", :js)

Unfortunately, this doesn't seem possible because the url is escaped further down the call chain (in url_for, I believe). What are the alternatives?

From stackoverflow

0 comments:

Post a Comment