简体   繁体   English

Rails 3:将参数传递给javascript函数时出错

[英]Rails 3: error while passing params to javascript function

I am trying to pass value to the JavaScript function but it gives me the below error on Firebug when I click the anchor tag (plus icon). 我正在尝试将值传递给JavaScript函数,但是当我单击锚标记(加号图标)时,它在Firebug上给了我以下错误。

What I am trying to do is, on click of a 'plus' sign, I add fields from partial to the table. 我想做的是,单击“加号”,将部分字段添加到表中。 What am I doing wrong here? 我在这里做错了什么?

Currently there is a table with '+' inside <th> . 当前在<th>有一个带有'+'的表。 I want to append data(from the partial) to the table. 我想将数据(从部分)追加到表中。

Error 错误

SyntaxError: syntax error
[Break On This Error]   

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

Helper file 助手文件

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

View file 查看文件

=add_fields 'Add', 'contact'

partial _add_fields.html.haml 部分_add_fields.html.haml

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

js file 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});

};

change the below line 更改下面的行

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

to

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

h() function is out dated in rails 3 h()函数在rails 3中已过期

use raw "string" 使用raw "string"

or "string".html_safe "string".html_safe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM