2014-06-18 41 views
0

我有我创建一个模板看起来像这样:为什么我的标记错误? - Ruby on Rails的

= link_to_function "#{image_tag('platform/icons/add.png')} Add another choice".html_safe, id: 'new-survey', tabindex: -1 do |page| 
    = f.fields_for :survey_choices, SurveyChoice.new do |sc| 
     - page.insert_html :bottom, 'survey-choices', partial: 'admin/survey_choices/form', locals: { form: sc } 

但这里是它创造的标记:

<a href="#" onclick="{:id=>"new-survey", :tabindex=>-1}; return false;"><img alt="Add" src="/assets/platform/icons/add.png"> Add another choice</a> 

为什么我的ID没有被渲染在页面上正确?我的js函数无法附加到它?我需要做什么才能使这个渲染正确?

回答

1

部分id: 'new-survey', tabindex: -1被视为Javascript函数。

更改您的link_to_function调用并在选项前添加实际功能。这样的事情:

link_to_function "#{image_tag('platform/icons/add.png')} Add another choice".html_safe, 
    'YOUR JAVASCRIPT FUNCTION CALL HERE', id: 'new-survey', tabindex: -1 
+0

我有':javascript'标记页面底部的所有javascript。如何从Ruby中调用函数?我宁愿能够直接附加到'id =“新调查”'。我可以这样做吗? – jhamm

+0

附加到ID将是更优雅的解决方案。只需使用'link_to'而不是'link_to_function',使用'#'作为链接目标,并使用JQuery将侦听器附加到元素。 –