javascript in a rails :url parameter

javascript in a rails :url parameter

像你 发布于 2021-11-28 字数 1236 浏览 637 回复 1 原文

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'>
      Droppables.add('#{droppable_id}', {accept:'lead', onDrop:function(element){new Ajax.Request('/leads/' + ('_').last()) + '.js', {asynchronous:true, evalScripts:true, method:'put', parameters:'lead[classification]=#{classification}&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')})}})

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

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


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?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


裸钻 2022-06-07 1 楼

What version of rails are you using? If you're on 2.3.8 then your "escaping" may simply be because of the "string addition bug":

You can fix it either by finding ingenious hacks around it (eg encasing your string in "#{my_string}" to re-evalute it) or by downgrading to 2.3.5.