rails中的javascript:url参数

时间:2009-04-27 16:33:19

标签: ruby-on-rails

我想创建一个drop_receiving_element,其中:url包含javascript。目前我刚刚创建了一个辅助函数并对元素进行了硬编码:

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
但是,这很丑陋和丑陋。理想情况下,我想做类似的事情:

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

或者

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

不幸的是,这似乎不可能,因为url会在调用链的下游进一步转义(我相信在url_for中)。有哪些替代方案?

1 个答案:

答案 0 :(得分:0)

您使用的是哪种版本的导轨?如果您使用的是2.3.8,那么您的“转义”可能只是因为“字符串添加错误”: https://rails.lighthouseapp.com/projects/8994/tickets/4695-string-added-to-rails_helpers-gets-html-escaped

你可以通过找到它周围的巧妙黑客来修复它(例如将你的字符串包含在“#{my_string}”中以重新评估它)或降级到2.3.5。