2013-04-11 50 views
-1

我想將值傳遞給JavaScript函數,但是當我單擊錨標記(加上圖標)時,它給了我Firebug上的以下錯誤。Rails 3:錯誤同時傳遞params到javascript函數

我想要做的是,點擊'加'符號,我將部分字段添加到表中。我在這裏做錯了什麼?

目前在<th>內有一個'+'表。我想將數據(從部分)附加到表格中。

錯誤

SyntaxError: syntax error 
[Break On This Error] 

add_fields(this, contact, &quot;&lt;tr class=\&#x27;fields\&#x27;&gt;\n&lt;td&gt... 

助手文件

module TestHelper 
    def add_fields(name, obj) 

     if (obj == 'contact') 
      fields = render :partial => '/test/add_fields', :locals => {:map_values => ["Name", "URL", "Address"]} 
      link_to_function(name, h("add_fields(this, #{obj}, \"#{escape_javascript(fields)}\")")) 

     elsif (obj=='company') 
      fields = render :partial => '/test/add_fields', :locals => {:map_values => ["CName", "CURL", "CAddress"]} 
      link_to_function(name, h("add_fields(this, #{obj}, \"#{escape_javascript(fields)}\")")) 
     elsif (obj=='lead') 
      fields = render :partial => '/test/add_fields', :locals => {:map_values => ["LName", "LURL", "LAddress"]} 
      link_to_function(name, h("add_fields(this, \"#{obj}\", \"#{escape_javascript(fields)}\")")) 
     end 

    end 
end 

查看文件

=add_fields 'Add', 'contact' 

個局部_add_fields.html.haml

%tr.fields 
    %td 
    %select{:class => 'select-two'} 
     - map_values.each do |e| 
     %option #{e} 

JS文件

add_fields = function(obj, maptype, content){ 

    //console.log(obj.closest('.table')); 
    var parent = $(obj).closest('.table'); 
    var msg_obj = $('.alert.alert-info',parent); 
    if(msg_obj.length > 0) { 
    msg_obj.closest('tr').addClass('hidden'); 
    } 

    $('tbody',parent).append(content); 
    $("select.select-two").select2({allowClear: true}); 

}; 

回答

2

變化下面線

link_to_function(name, h("add_fields(this, \"#{obj}\", \"#{escape_javascript(fields)}\")")) 

link_to_function("name", "add_fields(this, '#{obj}', '#{j(fields)}' 
)".html_safe) 

H()函數是過時的軌道3

使用raw "string"

"string".html_safe

+0

完美!謝謝 :) – anpsmn 2013-04-11 10:15:01